自定义View超出屏幕大小,在自定义View里面重写了onTouchEvent方法,请问,如何在case MotionEvent.ACTION_MOVE:中操作?我看网上说结合scrollTo和scrollBy方法,但也没说具体怎么结合,它们之间是如何调用了?
下面是我的代码:public class AndroidTest03 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView3(this));
}
}自定义View:public class MyView3 extends View {
int cur_x;
int cur_y;
Bitmap pic = null ;
public MyView3(Context ctx) {
super(ctx);
}
public MyView3(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
pic = BitmapFactory.decodeResource(getResources(),R.drawable.p2);
RectF dst = new RectF(_x, 0,maxWidth ,maxHeight );
canvas.drawBitmap(pic, null, dst, null) ;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(1680, 1050);//这样就超出了屏幕大小
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
cur_x = (int) event.getX();
cur_y = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
break;
}
return true;
}
@Override
public void scrollTo(int x, int y) {
super.scrollTo(x, y);
}
}补充:自定义View里面显示一张图片,并且该View的尺寸大于屏幕的尺寸,要想看到被遮住的地方,只能上下左右那么滑屏来显示。如果使用ScrollView的话,斜着滑不了了。
下面是我的代码:public class AndroidTest03 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView3(this));
}
}自定义View:public class MyView3 extends View {
int cur_x;
int cur_y;
Bitmap pic = null ;
public MyView3(Context ctx) {
super(ctx);
}
public MyView3(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
pic = BitmapFactory.decodeResource(getResources(),R.drawable.p2);
RectF dst = new RectF(_x, 0,maxWidth ,maxHeight );
canvas.drawBitmap(pic, null, dst, null) ;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(1680, 1050);//这样就超出了屏幕大小
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
cur_x = (int) event.getX();
cur_y = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
break;
}
return true;
}
@Override
public void scrollTo(int x, int y) {
super.scrollTo(x, y);
}
}补充:自定义View里面显示一张图片,并且该View的尺寸大于屏幕的尺寸,要想看到被遮住的地方,只能上下左右那么滑屏来显示。如果使用ScrollView的话,斜着滑不了了。
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
cur_x = (int) event.getX();
cur_y = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
int x = (int) event.getX();
int y = (int) event.getY();
moveView(x-cur_x, y-cur_y);
cur_x = x;
cur_y = y;
}
return true;
}
public void moveView(int offsetX, int offsetY){
if(offsetX ==0 && offsetY==0)
return;
int left = this.getLeft() + offsetX;
int top = this.getTop() + offsetY;
int right = this.getRight() + offsetX;
int bottom = this.getBottom() + offsetY;
this.layout(left, top, right, bottom);
}
float x=0,y=0;
float initialX,initialY;
float downX, downY;
Bitmap pic = null ; public LargeView(Context ctx) {
super(ctx);
loadPicture();
} public LargeView(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
loadPicture();
} /*
* 装载图片
*/
private void loadPicture(){
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.p2, opts); opts.inSampleSize = computeSampleSize(opts, -1, 800 * 800);
opts.inJustDecodeBounds = false; pic = BitmapFactory.decodeResource(getResources(), R.drawable.p2, opts);
}
/*
* 绘图
* @see android.view.View#onDraw(android.graphics.Canvas)
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int displayWidth = canvas.getWidth();
int displayHeight = canvas.getHeight();
int left = (int)x;
int top = (int)y;
//图片显示区域
Rect src = new Rect(left, top, left + displayWidth, top+displayHeight);
//屏幕显示区域
RectF dst = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
canvas.drawBitmap(pic, src, dst, null);
invalidate();
}
public static int computeSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,
maxNumOfPixels); int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
} return roundedSize;
}
private static int computeInitialSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight; int lowerBound = (maxNumOfPixels == -1) ? 1 :
(int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 :
(int) Math.min(Math.floor(w / minSideLength),
Math.floor(h / minSideLength)); if (upperBound < lowerBound) {
// return the larger one when there is no overlapping zone.
return lowerBound;
} if ((maxNumOfPixels == -1) &&
(minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(1680, 1050);//这样就超出了屏幕大小
}
@Override
public void scrollTo(int x, int y) {
Log.i("LargeView","scroll");
super.scrollTo(x, y);
} public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch(action){
case MotionEvent.ACTION_DOWN:
initialX = x;
initialY = y;
downX = event.getX();
downY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
x = initialX + event.getX() - downX;
y = initialY + event.getY() - downY;
break;
}
return true;
}
}