0

EditText の下のスペース全体が ImageView で覆われるように、カスタム ImageView を既に EditText を含む MainActivity に動的に追加したいと考えています。次に、TouchEvent 時に ImageView に何かを描画し、それに応じてテキストを EditText に出力します。しかし、私のアプローチでは何もうまくいきません。助けてください。
*アクティビティ_メイン.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

さて、最後にアプローチを変更しました(実際には最初に考えていたものでしたが、それでも満足できません)
EditTextの下に静的ImageViewを作成し、MotionEventsの処理と直接描画に使用しました。MotionEventの場合、getXとgetYを使用するとビュー全体を基準にした座標が得られるため、座標を正規化する必要がありましたが、ImageViewを基準にした座標が必要でした。このアプローチの欠点は、速度がやや遅いことです。そのため、使用を免除しました。しかし、今のところ、同じアクティビティでEditTextとカスタムImageViewの両方を処理する他のアプローチはありません。
任意の提案をいただければ幸いです。

于 2013-03-10T12:57:03.890 に答える
0

BI = 新しい BondImage(これ); レイアウト ビューに追加されていないため、表示されません

試す

setContentView(BI); 
于 2013-03-09T20:58:56.490 に答える