Android实现图片自动翻页+手指缩放的功能
之前的几篇博文介绍了Android中简单实现页面翻转和自动翻转的功能,Android中实现双指缩放的功能,Android中实现计时器的功能,现在我们来把它们综合使用一下,写一个模仿手机相册的功能,能够实现图片自动翻页+手指缩放的功能
1. 构建好手指缩放的类,Android中实现双指缩放的功能,本博文中有详细介绍,为了减少篇幅就不在这里一一解释了!
package com.example.mygesture;
import android.util.Log;
import android.view.View;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
public class DragScaleView extends View {
//监听图片缩放
private ScaleGestureDetector mScaleDetector;
//监听图片移动
private GestureDetector mGestureDetector;
private int viewWidth, viewHeight;
//组件尺寸只需要获取一次
private boolean hasGetViewSize;
//当前的缩放比例
private float mScaleFactor = 1.0f;
private Paint bmpPaint = new Paint();
//图片资源
private Bitmap bmp;
private float widthScale, heightScale;
//绘制图片的起始位置
private float mPosX, mPosY;
//图片的宽高
private int bmpWidth, bmpHeight;
public DragScaleView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
mScaleDetector = new ScaleGestureDetector(context, new SimpleScaleListenerImpl());
mGestureDetector = new GestureDetector(context, new SimpleGestureListenerImpl());
}
public void setImageResource(int id) {
bmp = BitmapFactory.decodeResource(getResources(), id);
bmpWidth = bmp.getWidth();
bmpHeight = bmp.getHeight();
Log.d("Data", "bmpWidth: " + bmpWidth + "\t" + "bmpHeight: " + bmpHeight);
initViewSize();
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (bmp == null) {
return;
}
if