感谢您的回复,我想我没有解释清楚,我想要的是在同一个画布上绘制和绘画。在下面的代码中,我试图拖动“mBitmap”,但我不能:(
class Panel extends SurfaceView implements SurfaceHolder.Callback {
private Bitmap mBitmap;
private ViewThread mThread;
private Path path = new Path();
private final int paintColor = Color.BLACK;
private Paint drawPaint;
private int mX;
private int mY;
public Panel(Context context) {
super(context);
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
getHolder().addCallback(this);
mThread = new ViewThread(this);
setupPaint();
}
public void doDraw(Canvas canvas) {
canvas.drawColor(Color.GREEN);
Paint paint = new Paint();
int ancho = canvas.getWidth();
int largo = canvas.getHeight();
paint.setARGB(255, 255, 0, 0);
paint.setStrokeWidth(6);
canvas.drawLine(0, (largo/2), ancho, (largo/2), paint);
canvas.drawBitmap(mBitmap, mX, mY, null);
canvas.drawPath(path, drawPaint);
}
private void setupPaint() {
drawPaint = new Paint();
drawPaint.setColor(paintColor);
drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(5);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (!mThread.isAlive()) {
mThread = new ViewThread(this);
mThread.setRunning(true);
mThread.start();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mThread.isAlive()) {
mThread.setRunning(false);
}
}
public boolean onTouchEvent(MotionEvent event) {
float pointX = event.getX();
float pointY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(pointX, pointY);
break;
case MotionEvent.ACTION_MOVE:
path.lineTo(pointX, pointY);
break;
default:
return false;
}
postInvalidate();
return true;
}
抱歉,但我不太了解。如果有人能指导我,我非常感激!
答案 0 :(得分:0)
以下是一些可以帮助您入门的代码:
<强> DragDropLinearLayout.java 强>
package com.example.views;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class DragDropLinearLayout extends LinearLayout {
private static final String TAG = DragDropLinearLayout.class.getSimpleName();
private boolean dragging = false;
private View draggedView = null;
private float touchX, touchY;
public DragDropLinearLayout(Context context) {
super(context);
}
public DragDropLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void addView(View v, int index, ViewGroup.LayoutParams params) {
super.addView(v, index, params);
v.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
dragging = true;
draggedView = v;
Log.d(TAG, "Dragging started!");
return true;
}
});
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
if (dragging) {
canvas.save();
canvas.translate(touchX - draggedView.getWidth(), touchY - draggedView.getHeight());
canvas.scale(1.5f, 1.5f);
canvas.clipRect(0, 0, draggedView.getWidth(), draggedView.getHeight());
draggedView.draw(canvas);
canvas.restore();
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_DOWN:
touchX = event.getX();
touchY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
break;
}
return dragging || super.onInterceptTouchEvent(event);
}
public void onDragComplete() {
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
dragging = false;
onDragComplete();
invalidate();
Log.d(TAG, "Dragging stopped!");
break;
case MotionEvent.ACTION_DOWN:
touchX = event.getX();
touchY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
touchX = event.getX();
touchY = event.getY();
if (dragging) {
getParent().requestDisallowInterceptTouchEvent(true);
invalidate();
}
break;
}
return dragging || super.onTouchEvent(event);
}
}
<强> activity_main.xml中强>
<?xml version="1.0" encoding="utf-8"?>
<com.example.views.DragDropLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootView"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
</com.example.views.DragDropLinearLayout>
这是做什么
此示例使用名为LinearLayout
的自定义DragDropLinearLayout
实现。它允许您拖放添加到LinearLayout的单个ImageView。
如何运作
所有繁重的工作都由DragDropLinearLayout
完成。 DragDropLinearLayout
的作用是首先将OnLongClickListener添加到添加到其中的所有子项。侦听器将当前被长时间单击的任何视图标记为当前正在拖动的视图。
它还具有触摸侦听器,以便它可以确定用户当前拖动视图的位置。它最终绘制了用户手指所在位置拖动视图的克隆。