Setting Text to Image On Android and Adjudt the text font size based on the android resolution
2012-02-21 16:47
429 查看
1. Setting Text to Image On Android
2. Adjudt the text font size based on the android resolution
3. 文字居中: paint.setTextAlign(Align.CENTER);然后drawText的x坐标设置为width / 2即可
private float toPixel(Resources res, int dip)
{
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dip, res.getDisplayMetrics());
return px;
}
public Bitmap createBitmap(Bitmap src, String str)
{
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int w = src.getWidth();
int h = src.getHeight();
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;
int height = (int)960*68/screenHeight;
Bitmap bmpTemp = Bitmap.createBitmap(w, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bmpTemp);
Paint p = new Paint();
Typeface font = Typeface.create(Typeface.DEFAULT,Typeface.BOLD);
p.setColor(Color.GRAY);
p.setTypeface(font);
int fontSize = (int)toPixel(getResources(),11);
p.setTextSize(fontSize);
p.setTextAlign(Align.CENTER);
canvas.drawBitmap(src, 0, 0, p);
int y = (int)960 *63/screenHeight;
int x = (int)screenWidth/10;
int adjustWidth = (int)(screenWidth/10 - (screenWidth - 5 *w)/10);
if (adjustWidth < x
&& adjustWidth > 0)
{
x = adjustWidth;
}
canvas.drawText(str,x , y, p);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
return bmpTemp;
}
2. Adjudt the text font size based on the android resolution
3. 文字居中: paint.setTextAlign(Align.CENTER);然后drawText的x坐标设置为width / 2即可
private float toPixel(Resources res, int dip)
{
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dip, res.getDisplayMetrics());
return px;
}
public Bitmap createBitmap(Bitmap src, String str)
{
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int w = src.getWidth();
int h = src.getHeight();
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;
int height = (int)960*68/screenHeight;
Bitmap bmpTemp = Bitmap.createBitmap(w, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bmpTemp);
Paint p = new Paint();
Typeface font = Typeface.create(Typeface.DEFAULT,Typeface.BOLD);
p.setColor(Color.GRAY);
p.setTypeface(font);
int fontSize = (int)toPixel(getResources(),11);
p.setTextSize(fontSize);
p.setTextAlign(Align.CENTER);
canvas.drawBitmap(src, 0, 0, p);
int y = (int)960 *63/screenHeight;
int x = (int)screenWidth/10;
int adjustWidth = (int)(screenWidth/10 - (screenWidth - 5 *w)/10);
if (adjustWidth < x
&& adjustWidth > 0)
{
x = adjustWidth;
}
canvas.drawText(str,x , y, p);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
return bmpTemp;
}
相关文章推荐
- Android to the picture plus text and image watermarking example code
- how to put the text on the left of a radio button in android
- Android check the net and go to the setting acvivity by intent
- 如何使用OpenCV的处理图像,文字变得锐利和清晰?(How to use OpenCV to process image so that the text become sharp and clea
- android Failed to install *.apk on device 'emulator-5554' timeout 或者 the user data image is used解决办法
- 论文阅读(Xiang Bai——【PAMI2017】An End-to-End Trainable Neural Network for Image-based Sequence Recognition and Its Application to Scene Text Recognition)
- [Android]:Setting up a Library Project and how to use the library (工程引用和dll引用)
- How to Create Text Image on the fly with ASP.NET
- Warning:The `android.dexOptions.incremental` property is deprecated and it has no effect on the buil
- What Is Bootloader And How To Unlock Bootloader On Android Phones
- How to Create Transportable Tablespaces Where the Source and Destination are ASM-Based
- How to resize a *.jpg image and save the result to a file
- [Android]The connection to adb is down, and a severe error has occured.
- How to change the resolution in text mode for Linux5+
- Android基础(2)error:The connection to adb is down, and a severe error has occured.
- How to dynamically display the tips or help on the text box
- How to change the size of the button of an android checkbox?
- unable to find attribute android:fontVariationSettings and android:ttcIndex
- WampDeveloper Pro is the best (and fastest) way to create and run multiple websites on Windows, usin
- MS bug "The connection pool" in Oracle 10g and the data sort according to specified filed on DataGrid control.