拖动图片

package com.test.Time;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class MyMapView extends View {
Paint paint;
float initx = 0;// 照片的绘画初始点
float inity = 0;
float lastx = 0;// 记录按下时的坐标
float lasty = 0;
float movex = 0;// 手指在屏幕移动的距离
float movey = 0;
float photoWidth;
float photoHeight;
float X;// 记录屏幕与照片的宽度差
float Y;// 记录屏幕与照片的高度差
float screenWidth;
float screenHeigth;
float drawx; // 照片的绘画坐标原点
float drawy;
WindowManager wm;
Bitmap bt;
public MyMapView (Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public MyMapView (Context context) {
super(context);
init(context, null);
}
private void init(Context context, AttributeSet attrs) {
paint = new Paint();
wm = ((Activity) context).getWindowManager();
File file = new File("/sdcard/test.jpg");
FileInputStream fis;
ByteArrayOutputStream output = null;
byte[] result;
try {
fis = new FileInputStream(file);
output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024 * 100];
int n = 0;
while (-1 != (n = fis.read(buffer))) {
output.write(buffer, 0, n);
}
result = output.toByteArray();
bt = BitmapFactory.decodeByteArray(result, 0, result.length);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
Display display = wm.getDefaultDisplay();
screenWidth = display.getWidth();
screenHeigth = display.getHeight();
photoWidth = bt.getWidth();
photoHeight = bt.getHeight();
setClickable(true);
setFocusableInTouchMode(true);
X = screenWidth - photoWidth;
Y = screenHeigth - photoHeight;
initx = (screenWidth - photoWidth) / 2;
inity = (screenHeigth - photoHeight) / 2;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
float dx;
float dy;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastx = event.getX();
lasty = event.getY();
break;
case MotionEvent.ACTION_MOVE:
dx = event.getX() - lastx;
dy = event.getY() - lasty;
movex = dx;
movey = dy;
invalidate();
break;
case MotionEvent.ACTION_UP:
if (X <= 0) {
initx += movex;
if (initx > 0)
initx = 0;
if (initx < X) {
initx = X;
}
}
if (Y <= 0) {
inity += movey;
if (inity > 0)
inity = 0;
if (inity < Y) {
inity = Y;
}
}
movex = 0;
movey = 0;
invalidate();
break;
}
return super.onTouchEvent(event);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (X <= 0 && initx + movex >= 0) {// 当X>0时,说明屏幕的宽度足够显示整张图片,不允许拖动
drawx = 0;//滑动到右边
} else if (X <= 0 && initx + movex <= X) {
drawx = X;
} else if (X <= 0) {//滑动中
drawx = initx + movex;
} else {
drawx = initx;
}
if (Y <= 0 && inity + movey >= 0) {
drawy = 0;
} else if (Y <= 0 && inity + movey <= Y) {
drawy = Y;
} else if (Y <= 0) {
drawy = inity + movey;
} else {
drawy = inity;
}
canvas.drawBitmap(bt, drawx, drawy, paint);
}


}


 

 

 

 

}

你可能感兴趣的:(图片)