How to show android bitmapdrawable change on next clickevents?

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??

  • Answer:

    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.

andromania at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.