Android 以编程方式在MainActivity中添加自定义图像视图

Android 以编程方式在MainActivity中添加自定义图像视图,android,imageview,android-imageview,android-custom-view,Android,Imageview,Android Imageview,Android Custom View,我想动态地将我的自定义ImageView添加到已包含EditText的MainActivity中,以便ImageView可以覆盖EditText下的整个空间。然后,我将在TouchEvent上的ImageView上绘制一些内容,并相应地将一些文本输出到EditText。但我的方法没有任何效果。请帮忙。 *activity_main.xml* <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

我想动态地将我的自定义ImageView添加到已包含EditText的MainActivity中,以便ImageView可以覆盖EditText下的整个空间。然后,我将在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>
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;        
}
}
BI=新的BondImage(本);尚未添加到布局视图中,因此其未显示

试一试


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

如果您有任何建议,我们将不胜感激。

您所说的“但我的方法没有任何效果”是什么意思?您是否有错误消息?它的运行时错误。消息:“应用程序已停止工作”显示完整的logcat输出。这里是:@Bond:move
bm=Bitmap.createBitmap(LayoutParams.MATCH\u PARENT,LayoutParams.MATCH\u PARENT,Config.ARGB\u 888)
rl.addView….
行之后,仍然得到相同的错误,然后传递一些静态高度/宽度,而不是
MATCH\u PARENT
,但我将其添加到BondImage类本身中!我也试过另一种方法。意味着将其添加到MainActivity类本身的布局中。这些方法都不管用。是的,这对ImageView是管用的,但是EditText是如何显示的呢?嗯,从这个实验中你没有意识到,当你打开view时,其他人没有地方可以容纳它,这意味着你先给家长打电话,所以其他人没有地方可以容纳Bond先生,但我见过一些应用程序是这样工作的。一定有办法!
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;        
}
}
setContentView(BI);