简体   繁体   中英

How to setBackgroundColor of textview to a Paint instead of a Color?

ok i know i can set a textview's background color by textview.setBackgroundColor(R.color.darkgrey) or some other color but im in a slight different situation. I have an object with three properties a Name, Value and a Paint. I used this object to draw a pie graph and have made a bunch of textview as legends for the graph. All i want to do now is set the background color to the Paint i used for the graph. I set the Paint by using the setARGB method of android.graphics.Paint. I have gone through the android notes trying to find a converting method to change the Paint to a color but no luck.

Any ideas how to setBackgroundColor() for a textview using Paint?

If you want to draw your text with canvas, you should do something like this:

public Bitmap myDrawText(Context context, String text, Typeface typeface, int  textSize, int width, int height) {
    Bitmap myBitmap = Bitmap.createBitmap(width, height,      Bitmap.Config.ARGB_8888);
    Canvas myCanvas = new Canvas(myBitmap);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setSubpixelText(true);
    paint.setTypeface(typeface);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);
    paint.setTextSize(textSize);
    paint.setTextAlign(Align.CENTER);
    myCanvas.drawText(text, (width / 2), 40, paint);
    return myBitmap;
}

Now you have a bitmap of your text!

    ImageView iv = findViewById(R.id.some_image);
    iv.setImageBitmap(myBitmap);

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