手指上的代码触摸在特定布局中绘制?

时间:2014-05-28 06:54:52

标签: android android-layout android-canvas

我想在手指移动后在布局上绘制线条。存在具有背景图像的父布局和具有id“relayForDraw”的相对布局的子,其具有一个图像视图。我想,当我的手指移动子布局时,然后在此上绘制线条。我应用了一些代码,但它不起作用 代码就在这里......

public class MainActivity extends Activity implements OnTouchListener {

RelativeLayout forDraw;
ImageView imgV;
Bitmap bitmap;
Canvas canvas;
Paint paint;

float downx = 0, downy = 0, upx = 0, upy = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    forDraw = (RelativeLayout) findViewById(R.id.relayForDraw);
    imgV = (ImageView) findViewById(R.id.imageView1);
    float dw = forDraw.getWidth();
    float dh = forDraw.getHeight();
    bitmap = Bitmap.createBitmap((int) dw, (int) dh,
            Bitmap.Config.ARGB_8888);
    canvas = new Canvas();
    paint = new Paint();
    paint.setColor(Color.GREEN);
    imgV.setImageBitmap(bitmap);
    imgV.setOnTouchListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        downx = event.getX();
        downy = event.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        upx = event.getX();
        upy = event.getY();
        canvas.drawLine(downx, downy, upx, upy, paint);
        imgV.invalidate();
        downx = upx;
        downy = upy;
        break;
    case MotionEvent.ACTION_UP:
        upx = event.getX();
        upy = event.getY();
        canvas.drawLine(downx, downy, upx, upy, paint);
        imgV.invalidate();
        break;
    case MotionEvent.ACTION_CANCEL:
        break;
    default:
        break;
    }
    return true;
}

}

我的xml文件在这里

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back1"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<RelativeLayout
    android:id="@+id/relayForDraw"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="112dp" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@null" />
</RelativeLayout>

它正在抛出此异常

java.lang.IllegalArgumentException: width and height must be > 0

1 个答案:

答案 0 :(得分:0)

你正在画一个空的&#39;画布。
替换:

canvas = new Canvas();

with:

canvas = new Canvas(bitmap);