使用java写一个Android自定义控件,包含加载图片,拖拽,缩放,涂鸦功能详细代码
时间: 2024-04-24 09:26:02 浏览: 177
当然,我将为您提供一个更详细的代码示例来实现加载图片、拖拽、缩放和涂鸦功能的自定义控件。以下是一个完整的示例:
```java
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
public class CustomImageView extends View {
private Bitmap imageBitmap;
private float imageX, imageY;
private float scaleFactor = 1.0f;
private Path drawingPath;
private Paint drawingPaint;
private float lastTouchX, lastTouchY;
private GestureDetector gestureDetector;
private ScaleGestureDetector scaleGestureDetector;
public CustomImageView(Context context) {
super(context);
init();
}
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
// 初始化画笔和绘制路径
drawingPaint = new Paint();
drawingPaint.setAntiAlias(true);
drawingPaint.setStyle(Paint.Style.STROKE);
drawingPaint.setStrokeWidth(5);
drawingPath = new Path();
// 初始化手势检测器
gestureDetector = new GestureDetector(getContext(), new MyGestureListener());
scaleGestureDetector = new ScaleGestureDetector(getContext(), new MyScaleGestureListener());
}
public void loadImage(int resId) {
// 加载图片
imageBitmap = BitmapFactory.decodeResource(getResources(), resId);
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 绘制图片
if (imageBitmap != null) {
canvas.save();
canvas.scale(scaleFactor, scaleFactor);
canvas.drawBitmap(imageBitmap, imageX, imageY, null);
canvas.restore();
}
// 绘制涂鸦
canvas.drawPath(drawingPath, drawingPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// 处理触摸事件
gestureDetector.onTouchEvent(event);
scaleGestureDetector.onTouchEvent(event);
float touchX = event.getX() / scaleFactor;
float touchY = event.getY() / scaleFactor;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 记录上一次的触摸位置
lastTouchX = touchX;
lastTouchY = touchY;
// 开始涂鸦
drawingPath.moveTo(touchX, touchY);
break;
case MotionEvent.ACTION_MOVE:
// 计算涂鸦路径
float dx = Math.abs(touchX - lastTouchX);
float dy = Math.abs(touchY - lastTouchY);
if (dx >= 4 || dy >= 4) {
drawingPath.quadTo(lastTouchX, lastTouchY, (touchX + lastTouchX) / 2, (touchY + lastTouchY) / 2);
lastTouchX = touchX;
lastTouchY = touchY;
}
break;
case MotionEvent.ACTION_UP:
// 结束涂鸦
drawingPath.lineTo(touchX, touchY);
break;
}
invalidate();
return true;
}
private class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// 实现拖拽功能的逻辑
imageX -= distanceX;
imageY -= distanceY;
invalidate();
return true;
}
}
private class MyScaleGestureListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
// 实现缩放功能的逻辑
scaleFactor *= detector.getScaleFactor();
scaleFactor = Math.max(0.1f, Math.min(scaleFactor, 5.0f));
invalidate();
return true;
}
}
}
```
在这个示例中,我们创建了一个名为 `CustomImageView` 的自定义控件。我们使用 `Bitmap` 类来存储加载的图片,并使用 `Path` 和 `Paint` 类来实现涂鸦功能。在触摸事件处理中,我们通过手势检测器来实现拖拽和缩放功能。
您可以在您的项目中使用此代码作为起点,并根据您的需求进行修改和扩展。希望对您有所帮助!如果您有任何其他问题,请随时提问。
阅读全文