我想将我的自定义 ImageView 动态添加到已经包含 EditText 的 MainActivity 中,这样 EditText 下的整个空间都应该被 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;
}
}