【发布时间】:2015-03-17 20:07:17
【问题描述】:
我正在从输入流中加载位图并将其放在图像视图上。然后我有一个按钮,应该在位图上画一个圆圈。错误是在 onClickListener 中找不到图像视图(称为“触摸”)......我该如何解决这个问题?当我按下按钮时,没有任何反应。 (注:ZoomInZoomOut 类是 Imageview 的扩展)
try {
java.io.FileInputStream in = this.openFileInput(path);
Bitmap bitmap = BitmapFactory.decodeStream(in);
ZoomInZoomOut touch = (ZoomInZoomOut)findViewById(R.id.IMAGEID);
touch = arrangeImageView(touch);
touch.setImageBitmap(bitmap);
in.close();
Button draw = (Button) findViewById(R.id.draw);
draw.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
Bitmap imageBitmap = Bitmap.createBitmap(200,
200, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(imageBitmap);
Paint p = new Paint();
p.setAntiAlias(true);
p.setColor(Color.BLUE);
canvas.drawCircle(60, 50, 25, p);
/////////////////////Error is on the next line:
touch.setImageBitmap(imageBitmap);
}
});
} catch (Exception e) {
e.printStackTrace();
}
这是 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@drawable/background"
tools:context="com.commonsware.android.test1.ImageDisplayActivity"
>
<com.commonsware.android.test1.ZoomInZoomOut
android:id="@+id/IMAGEID"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="o"
android:id="@+id/draw"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
【问题讨论】:
-
你也可以发布你的xml吗?
-
请查看更新后的帖子。
标签: java android button imageview onclicklistener