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

huangapple go评论78阅读模式
英文:

How to add text to a drawable

问题

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

}

英文:

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:

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.

答案1

得分: 0

感谢提供链接。非常有帮助。
原来我没有正确声明我的Bitmap。
这解决了问题。

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

这是一个已过时的方法,但它可以工作。

英文:

Thanks for the link. Very helpful.
Turns out i was not declaring my Bitmap correctly.

This solved the problem

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

Its a deprecated method, but it works.

huangapple
  • 本文由 发表于 2020年4月4日 09:44:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/61022814.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定