繁体   English   中英

如何在下次clickevents上显示android bitmapdrawable更改

[英]how to show android bitmapdrawable change on next clickevents

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

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上。 每次以前的文本被最新的替换?

每次调用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); 保持原样。 如果要将新文本放在现有文本下,则需要控制自己。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM