【问题标题】:how to show android bitmapdrawable change on next clickevents如何在下一个clickevents上显示android bitmapdrawable更改
【发布时间】:2015-04-15 10:22:53
【问题描述】:

当用户输入文本时,在下次单击时,图像上的先前文本消失了。如何在所有点击事件上将所有文本输入保存在可绘制的位图上。请提出建议。

onCreate() {

    ImageView mView = (ImageView) findViewById(R.id.dstImageView);
    Drawable myDrawable = getResources().getDrawable(R.drawable.pic2);
    mView.setImageDrawable(myDrawable);
    mView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                 if(event.getAction() == MotionEvent.ACTION_DOWN){
                float x = event.getX();
                float y = event.getY();
                showNameDialog();

    }
}

private void showNameDialog() {
    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("Photo Tag");
    alert.setMessage("Tag Message");
    // Set an EditText view to get user input 
    final EditText input = new EditText(this);
    alert.setView(input);
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
      Editable value = input.getText();
      // Do something with value!
        ImageView mView3 = (ImageView) findViewById(R.id.dstImageView); 
      myDrawable test=   writeTextOnDrawable(R.drawable.pic2,value.toString(),touchxpos,touchypos);
      mView3.setImageDrawable(myDrawabletest);
      }
    });

//  write text on drawable
private BitmapDrawable writeTextOnDrawable(int drawableId, String text,float xPos,float yPos) 
 {
     Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);
     Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);
     Paint paint = new Paint();
     paint.setStyle(Style.FILL);
     paint.setColor(Color.RED);
     paint.setTypeface(tf);
     paint.setTextAlign(Align.CENTER);
     paint.setTextSize(11);
     Rect textRect = new Rect();
     paint.getTextBounds(text, 0, text.length(), textRect);
     Canvas canvas = new Canvas(bm);
     canvas.drawText(text, xPos, yPos, paint);
     return new BitmapDrawable(getResources(), bm);
}

如何在每次点击时将文本保存在 bitmapdrawable 上。每次以前的文本都被最新的替换??

【问题讨论】:

    标签: android android-canvas android-imageview android-bitmap bitmapdrawable


    【解决方案1】:

    每次调用writeTextOnDrawable() 时,您都会返回一个新的Drawable,因此前一个会被丢弃。您需要有一个函数,您可以在其中传递现有的 Drawable 作为参考,如下所示:

    private BitmapDrawable writeTextOnDrawable(BitmapDrawable existingDrawable, int drawableId, String text,float xPos,float yPos) 
     {
     Bitmap bm;
     if (existingDrawable == null) {
      bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);
     } else {
      bm = existingDrawable.getBitmap();
     }
     Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);
     Paint paint = new Paint();
     paint.setStyle(Style.FILL);
     paint.setColor(Color.RED);
     paint.setTypeface(tf);
     paint.setTextAlign(Align.CENTER);
     paint.setTextSize(11);
     Rect textRect = new Rect();
     paint.getTextBounds(text, 0, text.length(), textRect);
     Canvas canvas = new Canvas(bm);
     canvas.drawText(text, xPos, yPos, paint);
     return new BitmapDrawable(getResources(), bm);
    }
    

    但是请注意,这只会在现有文本上写入文本,因为paint.getTextBounds(text, 0, text.length(), textRect); 保持不变。如果您希望将新文本置于现有文本之下,则需要自行控制。

    【讨论】:

    • 假设我有 res id,你能建议如何在我的活动类中定义一个 existingDrawable 吗?我正在寻找示例实现。
    • 你已经有了。这是您的myDrawable,您可以将其作为参数传递给我共享的函数。
    • 你的意思是这样吗? myDrawabletest= writeTextOnDrawable((BitmapDrawable) myDrawable,R.drawable.pic2,value.toString(),touchxpos,touchypos);问题是 myDrawable 的类型是 Drawable 而不是 BitmapDrawable。所以它失败了。类型转换没有帮助。
    • myDrawable 也是在 onCreate() 中定义的,我无法在 showNameDialog() 中访问它。在它里面你建议调用。你能告诉如何访问它吗?
    • 我的示例实现:public class phototag extends Activity { private int imgHeight, imgWidth;位图 bmp;可绘制 myDrawable = getResources().getDrawable(R.drawable.pic2); // 现在全局 *************************** 调用 myDrawable = writeTextOnDrawable((BitmapDrawable)myDrawable,R.drawable.pic2,value.toString (),touchxpos,touchypos); // 类型转换.. 应用程序在启动时崩溃.. 你能告诉这里有什么问题吗???
    猜你喜欢
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 2019-04-21
    相关资源
    最近更新 更多