0

我想将我的自定义 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;        
}
}
4

2 回答 2

0

BI = new BondImage(this); 尚未添加到布局视图中,它没有显示

尝试

setContentView(BI); 
于 2013-03-09T20:58:56.490 回答
0

好的,最后我修改了我的方法(实际上这是我最初的想法,但我仍然对此不满意)
我在 EditText 下方创建了一个静态 ImageView 并将其用于处理 MotionEvents 和直接绘图。对于 MotionEvent,我必须标准化坐标,因为简单地使用 getX 和 getY 会给出相对于整个视图的坐标但我需要它们相对于 ImageView 。这种方法的缺点是它有点慢,这就是我为什么不使用它的原因。但是现在我没有其他方法可以在同一活动中同时处理 EditText 和自定义 ImageView。
任何建议将不胜感激。

于 2013-03-10T12:57:03.890 回答