使用
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp"> <com.cuiweiyou.piccomp.ui.TextImageView android:id="@+id/imgView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:scaleType="fitCenter" android:background="@null"/> </RelativeLayout> |
自定义View
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
public class TextImageView extends AppCompatImageView { @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { Drawable drawable = getDrawable(); if (drawable != null) { int view_width = MeasureSpec.getSize(widthMeasureSpec); int view_height = MeasureSpec.getSize(heightMeasureSpec); int pic_width = drawable.getIntrinsicWidth(); int pic_height = drawable.getIntrinsicHeight(); // 根据图片的宽高比例设置View的宽高正好不大不小 if (pic_width > pic_height) { float pic_scale = 1.0f * pic_height / pic_width; float height_2 = view_width * pic_scale; view_height = (int) Math.ceil(height_2); setMeasuredDimension(view_width, view_height); } else { float view_scale = 1.0f * pic_width / pic_height; float width_2 = 1.0f * view_height * view_scale; view_width = (int) Math.ceil(width_2); setMeasuredDimension(view_width, view_height); } } else { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } //int width = getWidth(); // 初始为0,须刷新一下才有值 //int height = getHeight(); // //int view_width = MeasureSpec.getSize(widthMeasureSpec); //int view_height = MeasureSpec.getSize(heightMeasureSpec); // //Log.e("ard", "+++控件宽:" + width + "," + view_width); //Log.e("ard", "+++控件高:" + height + "," + view_height); } public Bitmap getBitmap() { int view_width = getWidth(); // 屏幕上控件的宽高像素 int view_height = getHeight(); int i = 0; if (i == 0) { /* 方1 * 源图片尺寸多大,生成的图片尺寸就多大,但文件会更大些 */ int textSize_tmp = textSize; // 文字大小 float touch_x_tmp = touch_x; // 文字位置 float touch_y_tmp = touch_y; Bitmap bitmap = ((BitmapDrawable) getDrawable()).getBitmap(); // 原始bitmap是没有文字的 bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); int src_width = bitmap.getWidth(); int src_height = bitmap.getHeight(); float scale_x = 1.0f * src_width / view_width; float scale_y = 1.0f * src_height / view_height; // * 根据屏幕显示的文字位置,计算在原始大小bitmap上的绘制位置和尺寸*/ touch_x = touch_x * scale_x; touch_y = touch_y * scale_y; textSize = (int) (textSize * scale_x); paint2.setTextSize(textSize); paint.setTextSize(textSize); Rect rect = new Rect(); paint.getTextBounds(textString, 0, textString.length(), rect); txt_width = rect.width(); txt_height = rect.height(); // * 绘制*/ Canvas canvas = new Canvas(bitmap); drawTxt(canvas); // 把文字写上 // * 复位显示*/ touch_x = touch_x_tmp; touch_y = touch_y_tmp; textSize = textSize_tmp; paint2.setTextSize(textSize); paint.setTextSize(textSize); rect = new Rect(); paint.getTextBounds(textString, 0, textString.length(), rect); txt_width = rect.width(); txt_height = rect.height(); return bitmap; } else if (i == 1) { /* 方法2 * 生成的图片和屏幕上控件的大小像素一致。有文字 */ boolean cacheEnabled = isDrawingCacheEnabled(); setDrawingCacheEnabled(true); Bitmap bitmap = getDrawingCache(); // setDrawingCacheEnabled(cacheEnabled); // 设为false,bitmp会无效。 return bitmap; } return null; } private void drawTxt(Canvas canvas){ if (!textString.equals("")) { canvas.drawText(textString, touch_x - txt_width / 2, touch_y - txt_height / 2, paint2); canvas.drawText(textString, touch_x - txt_width / 2, touch_y - txt_height / 2, paint); } } } |
- end
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/4131.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设