import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final int SCALEBASIC = 3; // 放大比率的调整倍数
private Bitmap bitmap = null; // 通过Bitmap控制图片
private ImageView img = null; // 定义显示图片
private SurfaceHolder holder = null; // SurfaceHolder对象
private int screenWidth = 0; // 保存屏幕宽度
private int screenHeight = 0; // 保存屏幕高度
private int imageWidth = 0; // 图片宽度
private int imageHeight = 0; // 图片高度
private int imageX = 0; // 保存图片开始的X轴
private int imageY = 0; // 保存图片开始的Y轴
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.requestWindowFeature(Window.FEATURE_NO_TITLE); // 不显示标题
this.screenWidth = super.getWindowManager().getDefaultDisplay()
.getWidth(); // 取得屏幕宽度
this.screenHeight = super.getWindowManager().getDefaultDisplay()
.getHeight(); // 取得屏幕高度
this.img = new ImageView(this); // 定义ImageView
this.bitmap = BitmapFactory.decodeResource(super.getResources(),
R.drawable.cena1); // 取得Bitmap
this.imageWidth = this.bitmap.getWidth(); // 图片宽度
this.imageHeight = this.bitmap.getHeight(); // 图片高度
this.imageX = (this.screenWidth - this.imageWidth) / 2; // 图片开始的X轴
this.imageY = (this.screenHeight - this.imageHeight) / 2; // 图片开始的Y轴
this.img.setImageBitmap(this.bitmap); // 显示图片
super.setContentView(new MySurfaceView(this)); // 使用SurfaceView封装
}
private class MySurfaceView extends SurfaceView implements
SurfaceHolder.Callback { // 实现SurfaceView
public MySurfaceView(Context context) {
super(context);
MainActivity.this.holder = super.getHolder(); // 取得SurfaceHolder
MainActivity.this.holder.addCallback(this); // 加入Callback操作
super.setFocusable(true); // 可以接收触摸事件
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) { // SurfaceView改变
}
@Override
public void surfaceCreated(SurfaceHolder holder) { // SurfaceView创建
MainActivity.this.setImage(
MainActivity.this.getScale(10, 10)
, 350, 500); // 设置默认显示图片
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) { // 销毁
}
}
private void setImage(float scale, int width, int height) { // 设置图片
Canvas canvas = MainActivity.this.holder.lockCanvas(null);// 获取画布
Paint paint = new Paint(); // 填充底色
canvas.drawRect(0, 0, MainActivity.this.screenWidth,
MainActivity.this.screenHeight, paint); // 绘制矩形
Matrix matrix = new Matrix(); // 控制图象
matrix.postScale(scale, scale); // 缩放设置
Bitmap target = Bitmap.createBitmap(MainActivity.this.bitmap, 0, 0,
width, height, matrix, true); // 创建新图片
this.imageWidth = target.getWidth(); // 取得新图片宽度
this.imageHeight = target.getHeight(); // 取得新图片高度
this.imageX = (this.screenWidth - this.imageWidth) / 2; // 重新计算X坐标
this.imageY = (this.screenHeight - this.imageHeight) / 2; // 重新计算Y坐标
canvas.translate(this.imageX, this.imageY); // 图象平移
canvas.drawBitmap(target, matrix, null); // 重新绘图
MainActivity.this.holder.unlockCanvasAndPost(canvas);// 解锁画布,提交画好的图像
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int pointCount = event.getPointerCount(); // 取得触控点的数量
if (pointCount == 2) { // 有两个触控点
float pointA = event.getY(0); // 取得第一个触摸的Y坐标
float pointB = event.getY(1); // 取得第二个触摸的Y坐标
if (pointA < pointB) { // 让pointA保存最大点
float temp = pointA;
pointA = pointB;
pointB = temp;
}
if (event.getAction() != MotionEvent.ACTION_UP) { // 用户按下
float scale = this.getScale(pointA, pointB) / SCALEBASIC;// 计算缩放量
MainActivity.this.setImage(scale, 350, 500); // 重设图片
}
}
return super.onTouchEvent(event);
}
private float getScale(float pointA, float pointB) {
float scale=pointA/pointB;
return scale;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
|
|