1

在创建剪贴画调色板时需要将不同的按钮拖放到视图中。我添加了我想要的图像作为按钮的背景。我尝试了代码,它运行良好,但问题是每次单击按钮都会丢弃相同的图像。这是我的代码

    layout = (FrameLayout) findViewById(R.id.LinearLayout01);
    // layout.setOnTouchListener(this);

    btn = (Button) findViewById(R.id.btn);
    btn.setDrawingCacheEnabled(true);
    btn.setOnTouchListener(this);

    btn2 = (Button) findViewById(R.id.btn2);
    btn2.setDrawingCacheEnabled(true);
    btn2.setOnTouchListener(this);

    btn3 = (Button) findViewById(R.id.btn3);
    btn3.setDrawingCacheEnabled(true);
    btn3.setOnTouchListener(this);


    btn4 = (Button) findViewById(R.id.btn4);
    btn4.setDrawingCacheEnabled(true);
    btn4.setOnTouchListener(this);


    params = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);


    }


@Override
public boolean onTouch(View view, MotionEvent me) {
             if (me.getAction() == MotionEvent.ACTION_DOWN) {
        status = START_DRAGGING;
        image = new ImageView(this);
        image.setImageBitmap(btn.getDrawingCache());
        layout.addView(image, params);

    }
    if (me.getAction() == MotionEvent.ACTION_UP) {
        status = STOP_DRAGGING;
        Log.i("Drag", "Stopped Dragging");
    } else if (me.getAction() == MotionEvent.ACTION_MOVE) {
        if (status == START_DRAGGING) {
            System.out.println("Dragging");
            image.setPadding((int) me.getRawX(), (int) me.getRawY(), 0, 0);
            image.invalidate();
        }
    }
            return false;
}  
4

0 回答 0