Android图片查看器

来源:互联网 发布:上海公交实时查询软件 编辑:程序博客网 时间:2024/05/01 07:28

/**
 *本程序由070203110安树峰软件0705倡导,主编,贺鹏,贾飞,
 *田镇源, 王振兴合作开发,仿冒必究
 */
package com.csdn.shufeng1988;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

/**
 * @author Shufeng An
 * @version 1.0.1
 *
 */
public class GameView extends View implements Runnable
{
 // 指示是否有鼠标点击
 boolean flag = false;

 Bitmap mBitQQ = null;
 int BitQQwidth = 0;
 int BitQQheight = 0;
 // 旋转角度
 float Angle = 0.0f;
 // 放大倍数
 float Scale = 1.0f;

 //定义旋转矩阵
 Matrix mMatrix = new Matrix();

 /* 定义Alpha动画 */
 private Animation mAnimationAlpha = null;

 /* 定义Scale动画 */
 private Animation mAnimationScale = null;

 /* 定义Translate动画 */
 private Animation mAnimationTranslate = null;

 /* 定义Rotate动画 */
 private Animation mAnimationRotate = null;
   
 //是否播放动画
 boolean flag2 = false;

 public GameView(Context context)
 {
  super(context);
  mBitQQ = ((BitmapDrawable) getResources().getDrawable(R.drawable.qq))
    .getBitmap();

  BitQQwidth = mBitQQ.getWidth();
  BitQQheight = mBitQQ.getHeight();

  new Thread(this).start();
 }

 /*
  * (non-Javadoc)
  *
  * @see java.lang.Runnable#run()
  */
 @Override
 public void run()
 {
  while (!Thread.currentThread().isInterrupted())
  {
   try
   {
    Thread.sleep(100);
   }
   catch (InterruptedException e)
   {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   postInvalidate();
  }

 }

 // 绘制图像在屏幕上
 public void onDraw(Canvas canvas)
 {
  // TODO Auto-generated method stub
  super.onDraw(canvas);

  mMatrix.reset();
  mMatrix.setRotate(Angle);
  mMatrix.postScale(Scale, Scale);

  // 克隆一张图片
  Bitmap mBitQQ2 = Bitmap.createBitmap(mBitQQ, 0, 0, BitQQwidth,
    BitQQheight, mMatrix, true);
  
  if (flag)
  {
   GameView.drawImage(canvas, mBitQQ2, x, y);
   flag2 = false;
  }
  else if (flag2)
  {
   canvas.drawBitmap(mBitQQ2, x, y, null);
   //flag = false;
  }
  else
  {
   GameView.drawImage(canvas, mBitQQ2, (320 - BitQQwidth) / 2 - 20, 50);
  }
  mBitQQ2 = null;

 }

 // 按下2,3,4,5键播放对应的动画
 // 按下上下左右键对应图片的缩小,放大,旋转
 public boolean onKeyDown(int keyCode, KeyEvent event)
 {
  switch (keyCode)
  {
  case KeyEvent.KEYCODE_DPAD_LEFT:
   Angle--;
   break;
  case KeyEvent.KEYCODE_DPAD_RIGHT:
   Angle++;
   break;
  case KeyEvent.KEYCODE_DPAD_UP:
   if (Scale > 0.15)
   {
    Scale -= 0.1;
   }
   break;   
  case KeyEvent.KEYCODE_DPAD_DOWN:
   if (Scale < 3)
   {
    Scale += 0.1;
   }
   break;
   
  case KeyEvent.KEYCODE_2:
   flag2 = true;
   /* 创建Alpha动画 */
   mAnimationAlpha = new AlphaAnimation(0.1f, 1.0f);
   /* 设置动画的时间 */
   mAnimationAlpha.setDuration(3000);
   /* 开始播放动画 */
   this.startAnimation(mAnimationAlpha);
   break;
  case KeyEvent.KEYCODE_3:
   flag2 = true;
   /* 创建Scale动画 */
   mAnimationScale = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
     Animation.RELATIVE_TO_SELF, 0.5f,
     Animation.RELATIVE_TO_SELF, 0.5f);
   /* 设置动画的时间 */
   mAnimationScale.setDuration(500);
   /* 开始播放动画 */
   this.startAnimation(mAnimationScale);
   break;
  case KeyEvent.KEYCODE_4:
   flag2 = true;
   /* 创建Translate动画 */
   mAnimationTranslate = new TranslateAnimation(10, 100, 10, 100);
   /* 设置动画的时间 */
   mAnimationTranslate.setDuration(1000);
   /* 开始播放动画 */
   this.startAnimation(mAnimationTranslate);
   break;
  case KeyEvent.KEYCODE_5:
   flag2 = true;
   /* 创建Rotate动画 */
   mAnimationRotate = new RotateAnimation(0.0f, +360.0f,
     Animation.RELATIVE_TO_SELF, 0.5f,
     Animation.RELATIVE_TO_SELF, 0.5f);
   /* 设置动画的时间 */
   mAnimationRotate.setDuration(1000);
   /* 开始播放动画 */
   this.startAnimation(mAnimationRotate);
   break;

  }
  return super.onKeyDown(keyCode, event);
 }

 // 当用户点击屏幕时,显示图片的坐标
 private float x = 0.0f;
 private float y = 0.0f;

 @Override
 public boolean onTouchEvent(MotionEvent event)
 {
  x = event.getRawX();
  // y坐标减去50px,消除像素误差
  y = event.getRawY() - 50;
  flag = true;

  return super.onTrackballEvent(event);
 }
   
 //自定义函数用来画转换后的图
 public static void drawImage(Canvas canvas, Bitmap bitmap, float x, float y)
 {
  canvas.drawBitmap(bitmap, x, y, null);
 }
}
 

 

//

package com.csdn.shufeng1988;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Activity01 extends Activity
{
 // 检查程序用
 private static final String TAG = "Activity01";

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
        
  //图片浏览按钮
  Button button = (Button) findViewById(R.id.Button01);
  //功能介绍按钮
  Button button2 = (Button) findViewById(R.id.Button02);
  button.setTextColor(Color.RED);
  button.setBackgroundColor(Color.GREEN);
  button.setTextSize(20);
  button2.setTextColor(Color.CYAN);
  button.setTextSize(30);
  //小组成员介绍按钮
  Button button3 = (Button) findViewById(R.id.Button03);
  button3.setTextColor(Color.GREEN);
  button3.setWidth(200);
  button3.setTextSize(40);
  button3.setBackgroundDrawable(getResources().getDrawable(
    R.drawable.shufeng));
  button.setOnClickListener(new Button.OnClickListener()
  {
   @Override
   public void onClick(View v)
   {
    Intent intent = new Intent();
    intent.setClass(Activity01.this, Activity02.class);
    startActivity(intent);
    Activity01.this.finish();

   }

  });

  button2.setOnClickListener(new Button.OnClickListener()
  {

   @Override
   public void onClick(View v)
   {
    Toast toast = Toast.makeText(Activity01.this,
      "按上下左右键对应缩小放大顺,逆时针旋转" +
      "按2,3,4,5,对应渐变、伸缩、转换、旋转动画"
      +"点击鼠标可以移动图片位置到指定位置",
      Toast.LENGTH_LONG);
    toast.setGravity(Gravity.TOP, 0, 390);
    toast.show();

    Dialog dialog = new AlertDialog.Builder(Activity01.this)
      .setTitle("提示").setMessage(
        "按上、下、左、右键对应缩小、放大、顺,逆时针旋转"
          + "按2、3、4、5,对应渐变、伸缩、转换、旋转动画"
          + "点击鼠标可以移动图片位置到指定位置")
      .setPositiveButton("确定",
        new DialogInterface.OnClickListener()
        {

         @Override
         public void onClick(DialogInterface dialog,
           int which)
         {
          dialog.cancel();

         }
        }).create();
    dialog.show();
   }
  });

  button3.setOnClickListener(new Button.OnClickListener()
  {

   @Override
   public void onClick(View v)
   {

    Dialog dialog = new AlertDialog.Builder(Activity01.this)
      .setTitle("提示").setMessage(
        "070203110安树峰,070203101田镇源,070203090贾飞"
          + "070203089贺鹏,070203150王振兴")
      .setPositiveButton("确定",
        new DialogInterface.OnClickListener()
        {

         @Override
         public void onClick(DialogInterface dialog,
           int which)
         {
          dialog.cancel();

         }
        }).create();
    dialog.show();
   }

  });
 }

 public void onStart()
 {
  super.onStart();
  Log.v(TAG, "onStart");
 }

 public void onResume()
 {
  super.onResume();
  Log.v(TAG, "onResume");

 }

 public void onPause()
 {
  super.onPause();
  Log.v(TAG, "onPause");
 }

 public void onStop()
 {
  super.onStop();
  Log.v(TAG, "onStop");
 }

 public void onDestroy()
 {
  super.onDestroy();
  Log.v(TAG, "onDestroy");
 }

 public void onRestart()
 {
  super.onRestart();
  Log.v(TAG, "onReStart");
 }
}

 

package com.csdn.shufeng1988;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;

public class Activity02 extends Activity
{

 private GameView mGameView = null;
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  
  mGameView = new GameView(this);
  
  setContentView(mGameView);
 }
 public boolean onKeyUp(int keyCode, KeyEvent event)
 {
  super.onKeyUp(keyCode, event);
  return true;
 }
 public boolean onKeyDown(int keyCode, KeyEvent event)
 {
  if ( mGameView == null )
  {
   return false;
  }
  if ( keyCode ==  KeyEvent.KEYCODE_BACK)
  {
   this.finish();
   return true;
  }
  return mGameView.onKeyDown(keyCode,event);
 }
}
 

原创粉丝点击