简体   繁体   English

Android OnDraw和onTouchEvent无法一起使用

[英]Android OnDraw and onTouchEvent not working together

I am new to android development as well as the Java programming language and would definitely appreciate some help with this issue. 我是android开发以及Java编程语言的新手,因此一定会对这个问题有所帮助。

Currently I have some code which creates a canvas and draws a bitmap picture onto it, this appears to work. 目前,我有一些代码可以创建画布并在其上绘制位图图片,这似乎可以正常工作。 I also have another bit of code which detects where the users finger is on the devices screen and this also works. 我还有另一段代码可以检测用户手指在设备屏幕上的位置,这也可以工作。 when I try to combine the two the bitmap will display but as soon as I click the screen the app crashes. 当我尝试将两者结合时,将显示位图,但是一旦我单击屏幕,应用程序就会崩溃。

I have a feeling that this issue is with the canvas/bitmap side of this as i have also noticed that any widgets that have added to layout vanish when the canvas/bitmap feature is running. 我感觉这个问题与画布/位图有关,因为我还注意到,运行画布/位图功能时,添加到布局的所有小部件都消失了。

This is the code for the canvas/bitmap: 这是画布/位图的代码:

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);

}

} }

and this is the code for the finger location on the screen: 这是手指在屏幕上的位置的代码:

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);
}

} }

Your view hierarchy does not have an EditText with the id editText1 . 您的视图层次结构没有ID为editText1的EditText。 This is because you have set the content view to be a single instance of your class MyView (class names should start with uppercase) with this code in onCreate() : 这是因为您已使用onCreate()以下代码将内容视图设置为类MyView的单个实例(类名应以大写字母开头onCreate()

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

As a result, you get a NullPointerException on the line txt.setText("X = "+ x +" Y = " + y); 结果,您在txt.setText("X = "+ x +" Y = " + y);行上得到了NullPointerException txt.setText("X = "+ x +" Y = " + y); because the previous line cannot find the EditText. 因为上一行找不到EditText。

You need to include both views in the layout. 您需要在布局中包括两个视图。 You can use MyView in XML by writing its fully qualified name. 您可以通过编写完全限定的名称来在XML中使用MyView For example: 例如:

<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