简体   繁体   English

如何将文本添加到可绘制对象

[英]How to add text to a drawable

I want to edit an image by adding text on top of an existing image.我想通过在现有图像上添加文本来编辑图像。 I tried this, but the drawable in the ImageView disappears:我试过这个,但 ImageView 中的可绘制对象消失了:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    Button btnAdd=(Button) findViewById(R.id.btnAdd);
    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText firstNum=(EditText)findViewById(R.id.fisrtNum);
            EditText secondNum=(EditText)findViewById(R.id.secondNum);
            TextView sumTV=(TextView)findViewById(R.id.sumTV);
            ImageView picIV=(ImageView) findViewById(R.id.picIV);

            Bitmap bitmap = Bitmap.createBitmap(300, 200, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

            paint.setTextSize(42);
            paint.setTextAlign(Paint.Align.CENTER);
            canvas.drawText("hello world",150,30,paint);
            paint.setColor(Color.BLUE);
            canvas.drawCircle(50, 50, 10, paint);
            picIV.setImageBitmap(bitmap);

            picIV.setImageDrawable(new BitmapDrawable(getResources(), bitmap));
            sumTV.setText(result+"");

        }
    }
}

Thank you in advance for helping find a solution.提前感谢您帮助找到解决方案。 My goal is to be able to edit an image and to send it to other apps.我的目标是能够编辑图像并将其发送到其他应用程序。

Thanks for the link.感谢您的链接。 Very helpful.非常有帮助。 Turns out i was not declaring my Bitmap correctly.原来我没有正确声明我的 Bitmap。

This solved the problem这解决了问题

            picIV.buildDrawingCache();
            Bitmap bitmap= picIV.getDrawingCache();

Its a deprecated method, but it works.它是一种已弃用的方法,但它有效。

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

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