我想将自定义ImageView动态添加到MainActivity中,MainActivity已包含EditText,以便EditView下的整个空间应由ImageView覆盖。然后我会在TouchEvent上的ImageView上绘制一些东西,并相应地将一些文本输出到EditText。但是我的方法没有任何结果。请帮忙。
* activity_main.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"
tools:context=".MainActivity"
android:id="@+id/RL"
>
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:inputType="textMultiLine" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
BondImage BI;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BI = new BondImage(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.activity_main, menu);
return true;
}
BondImage.java
public class BondImage extends ImageView{
Canvas c;
Paint p;
Bitmap bm;
float x, y;
public BondImage(Context context) {
super(context);
// TODO Auto-generated constructor stub
bm = Bitmap.createBitmap(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT,Config.ARGB_8888);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.RL);
rl.addView(this, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
c = new Canvas(bm);
p = new Paint();
p.setColor(Color.MAGENTA);
x = y = 0;
}
public boolean onTouchEvent(MotionEvent me){
c.drawCircle(x, y, 25, p); //example
this.setImageBitmap(bm);
x ++; y ++;
/* and some other stuff */
return true;
}
}
答案 0 :(得分:0)
BI = new BondImage(this);尚未添加到未显示的布局视图
试
setContentView(BI);
答案 1 :(得分:0)