Add text to image in android programmatically

前端 未结 4 1230
走了就别回头了
走了就别回头了 2020-12-14 21:21

I want to make an application just like opening screen of android.I am dynamically adding images to the rows of tableLayout. I have only defined tableLayout in xml file and

4条回答
  •  太阳男子
    2020-12-14 21:58

    I m successfully implemented such problem of adding text on image. Just look at following code. First of take one view as Relative layout in that layout take ImageView after that EditText and after that button. Give each of a id. Write a loadBitmapFromView function below.

    public Bitmap loadBitmapFromView(View v) {
            Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Config.ARGB_8888);
            Canvas c = new Canvas(b);
            v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
            v.draw(c);
            return b;
        }
    

    and on click of button.

                   Bitmap bitmap = loadBitmapFromView(relativeLayout);
                    File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),         "folderName");
                    if (!dir.exists())
                        dir.mkdirs();
    
                    File file = new File(dir, "capture.jpg");
                    try {
                        FileOutputStream fos = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                        imageView.setImageBitmap(bitmap);    
                    } catch (Exception e) {
                        Log.e("ExpressionEditImageActivity", "Error, " + e);
                    }
    

    Enjoy...

    For more reference, refer below screenshot:

提交回复
热议问题