繁体   English   中英

Android OnDraw和onTouchEvent无法一起使用

[英]Android OnDraw and onTouchEvent not working together

我是android开发以及Java编程语言的新手,因此一定会对这个问题有所帮助。

目前,我有一些代码可以创建画布并在其上绘制位图图片,这似乎可以正常工作。 我还有另一段代码可以检测用户手指在设备屏幕上的位置,这也可以工作。 当我尝试将两者结合时,将显示位图,但是一旦我单击屏幕,应用程序就会崩溃。

我感觉这个问题与画布/位图有关,因为我还注意到,运行画布/位图功能时,添加到布局的所有小部件都消失了。

这是画布/位图的代码:

public class myView extends View{

Bitmap image;
DisplayMetrics metrics;
public myView(Context context) {
    super(context);
    metrics = context.getResources().getDisplayMetrics();
    // TODO Auto-generated constructor stub
    image = BitmapFactory.decodeResource(getResources(), R.drawable.bhead);

}
@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);

    int width = metrics.widthPixels;
    int height = metrics.heightPixels;

    Matrix matrix = new Matrix();
    float angle = 90;
    float imageCenterX = 50;
    float imageCenterY = 50;
    matrix.setRotate(angle, imageCenterX, imageCenterY);
    Bitmap temp;
    temp = BitmapFactory.decodeResource(getResources(), R.drawable.bhead);
    image = Bitmap.createScaledBitmap(temp, 100, 100,true);
    matrix.postTranslate(((width/2)-50), ((height/2)-100));
    canvas.drawBitmap(image, matrix, null);

}

}

这是手指在屏幕上的位置的代码:

public class MainActivity extends ActionBarActivity {

myView mview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mview = new myView(this);
    setContentView(mview);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    int x = (int)event.getX();
    int y = (int)event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_UP:
    }

    EditText txt = (EditText)findViewById(R.id.editText1);
    txt.setText("X = "+ x +" Y = " + y);

    return false;
}




@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 onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

您的视图层次结构没有ID为editText1的EditText。 这是因为您已使用onCreate()以下代码将内容视图设置为类MyView的单个实例(类名应以大写字母开头onCreate()

mview = new myView(this);
setContentView(mview);

结果,您在txt.setText("X = "+ x +" Y = " + y);行上得到了NullPointerException txt.setText("X = "+ x +" Y = " + y); 因为上一行找不到EditText。

您需要在布局中包括两个视图。 您可以通过编写完全限定的名称来在XML中使用MyView 例如:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.your.package.name.MyView
        android:id="@+id/my_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ... />
</LinearLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM