Yug*_*abu 28
使用以下代码来满足您的要求
Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.yourimage); // the original file yourimage.jpg i added in resources
Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
String yourText = "My custom Text adding to Image";
Canvas cs = new Canvas(dest);
Paint tPaint = new Paint();
tPaint.setTextSize(35);
tPaint.setColor(Color.BLUE);
tPaint.setStyle(Style.FILL);
cs.drawBitmap(src, 0f, 0f, null);
float height = tPaint.measureText("yY");
float width = tPaint.measureText(yourText);
float x_coord = (src.getWidth() - width)/2;
cs.drawText(yourText, x_coord, height+15f, tPaint); // 15f is to put space between top edge and the text, if you want to change it, you can
try {
dest.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/sdcard/ImageAfterAddingText.jpg")));
// dest is Bitmap, if you want to preview the final image, you can display it on screen also before saving
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
您必须在清单文件中使用以下权限.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)
对于我的设备,路径是/sdcard
访问外部SD卡,它可能因其他设备而异.有些设备可能有/mnt/sdcard
内置SD卡.在使用此代码之前,请检查它.
实际上我为其他一些问题编写了上面的代码,从相机中捕获后需要在照片上加盖时间戳.我给了你相同的解决方案,并根据你的具体要求进行了一些修改.
我希望你能理解这一点.如果您对代码有任何疑问,请随时询问.
我不确定这是最好的解决方案,但它可能会帮助你。
步骤 1:创建一个相对布局(或任何其他布局)并将您的图像设置为其背景。
步骤 2:现在添加一个 TextView,其宽度和高度为match_parent
,重力设置为top|center_horizontal
。
步骤 3:现在添加另一个按钮或任何其他布局控件,这些控件将触发用户确认。(您应该将此控件放在相对布局之外)。
步骤 4:如果用户已确认图像,那么您可以通过以下代码截取相对布局的屏幕截图:
v1.setDrawingCacheEnabled(true); //v1 is the object of your Relative layout
Bitmap bm = v1.getDrawingCache();
if (bm != null) {
//TODO: write the code for saving the image.
Toast toast = Toast.makeText(YourActivity.this, "image saved",
Toast.LENGTH_LONG);
toast.show();
} else {
Toast toast = Toast.makeText(YourActivity.this,
"No image saved.", Toast.LENGTH_LONG);
toast.show();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11580 次 |
最近记录: |