Стереть с помощью перетаскиваемого объекта - PullRequest
0 голосов
/ 11 июня 2018

Я пытаюсь создать приложение для Android, которое будет стирать части изображения.Я пытаюсь добиться этого с помощью перетаскиваемого объекта (изображение ластика).

Я уже реализовал функции перетаскивания изображения ластика в любое место приложения и стирания фонового изображения при перетаскивании пальца.Теперь я хочу соединить две функции так, чтобы фоновое изображение стиралось только при перетаскивании изображения ластика.Как я могу выполнить эту задачу?

Ниже я сделал следующее:

MainActivity.java

public class MainActivity extends AppCompatActivity {
private ViewGroup rootLayout;
private int _xDelta;
private int _yDelta;
private RelativeLayout pl;
private ImageView w1;
private boolean clicked1 = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MyCustomView myCustomView = new MyCustomView(MainActivity.this);

    w1 = (ImageView) findViewById(R.id.wand1);
    pl = (RelativeLayout) findViewById(R.id.coordAct);
    RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.coordAct);

    rootLayout.addView(myCustomView);

    RelativeLayout.LayoutParams layoutParams1w2 = new RelativeLayout.LayoutParams(getScreenWidth(), getScreenHeight() / 2);
    layoutParams1w2.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
    myCustomView.setLayoutParams(layoutParams1w2);

    w1.setOnTouchListener(new ChoiceTouchListener());
}

public static int getScreenWidth() {
    return Resources.getSystem().getDisplayMetrics().widthPixels;
}

public static int getScreenHeight() {
    return Resources.getSystem().getDisplayMetrics().heightPixels;
}

private final class ChoiceTouchListener implements View.OnTouchListener {
    public boolean onTouch(View view, MotionEvent event) {
        if (!clicked1){
            rootLayout = (ViewGroup) w1.getParent();
            if (rootLayout != null) {
                // detach the child from parent
                rootLayout.removeView(w1);
            }
            RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(300, 300);
            pl.addView(w1);

            w1.setLayoutParams(layoutParams1);
            clicked1 = true;
        }
        final int X = (int) event.getRawX();
        final int Y = (int) event.getRawY();

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                _xDelta = X - lParams.leftMargin;
                _yDelta = Y - lParams.topMargin;
                break;
            case MotionEvent.ACTION_MOVE:
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                        .getLayoutParams();
                layoutParams.leftMargin = X - _xDelta;
                layoutParams.topMargin = Y - _yDelta;
                layoutParams.rightMargin = -250;
                layoutParams.bottomMargin = -250;
                view.setLayoutParams(layoutParams);
                break;
        }
        rootLayout.invalidate();
        return true;
    }
}

MyCustomView.java

public class MyCustomView extends View {

private Bitmap sourceBitmap;
private Canvas sourceCanvas = new Canvas();
private Paint destPaint = new Paint();
private Path destPath = new Path();

public MyCustomView(Context context) {
    super(context);

    //converting drawable resource file into bitmap
    Bitmap rawBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.armissueserase);

    //converting bitmap into mutable bitmap
    sourceBitmap = Bitmap.createBitmap(rawBitmap.getWidth(), rawBitmap.getHeight(), Bitmap.Config.ARGB_8888);

    sourceCanvas.setBitmap(sourceBitmap);
    sourceCanvas.drawBitmap(rawBitmap, 0, 0, null);

    destPaint.setAlpha(0);
    destPaint.setAntiAlias(true);
    destPaint.setStyle(Paint.Style.STROKE);
    destPaint.setStrokeJoin(Paint.Join.ROUND);
    destPaint.setStrokeCap(Paint.Cap.ROUND);
    //change this value as per your need
    destPaint.setStrokeWidth(50);
    destPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
}

@Override
protected void onDraw(Canvas canvas) {
    sourceCanvas.drawPath(destPath, destPaint);
    canvas.drawBitmap(sourceBitmap, 0, 0, null);
    super.onDraw(canvas);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float xPos = event.getX();
    float yPos = event.getY();

    switch (event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
            destPath.moveTo(xPos, yPos);
            break;
        case MotionEvent.ACTION_MOVE:
            destPath.lineTo(xPos, yPos);
            break;
        default:
            return false;
    }
    invalidate();
    return true;
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/coordAct"
android:background="@drawable/background"
tools:context="com.erasewithtouch.MainActivity">

<RelativeLayout
    android:id="@+id/parLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3"
    android:background="#0000ff">



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="28dp"
        android:background="#BA9DF7"
        android:gravity="bottom"
        android:orientation="vertical">

        <HorizontalScrollView
            android:id="@+id/backgd"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:weightSum="1">

            <LinearLayout
                android:id="@+id/parentLL"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="horizontal"
                android:weightSum="5">


                    <ImageView
                        android:id="@+id/wand1"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_centerInParent="true"
                        android:layout_gravity="center"
                        android:src="@drawable/wand1" />


            </LinearLayout>
        </HorizontalScrollView>
    </RelativeLayout>


</RelativeLayout>

Любая помощь с благодарностью.Спасибо!

...