简体   繁体   中英

how to show android bitmapdrawable change on next clickevents

On next click when user inputs text, previous text on image is gone. How to save all text inputs on bitmap drawable on all click events. Please suggest.

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);
}

How do i save the texts on bitmapdrawable for each click. Every time the previous text is getting replaced with latest one??

Every time you call writeTextOnDrawable() you are returning a new Drawable so the previous one is getting discarded. You need to have a function where you can pass an existing Drawable as reference like so:

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);
}

Note however that this will just write text over the existing one because the paint.getTextBounds(text, 0, text.length(), textRect); remains the same. You will need to control that yourself if you want the new text to be put under the existing one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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