在做分享的时候出现一个问题是渐变色出现色块,查到的原因是TextView或者Button,IMageView等获取当前的控件生成的图片RGB是565,但是使用LInearLayout包一层获取外面一层的控件就会生成一个RGB8888的图片。
代码如下:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final View view = View.inflate(this, R.layout.activity_main, null);
TextView textView = view.findViewById(R.id.textview1);
TextView button = view.findViewById(R.id.button);
LinearLayout textView2 = view.findViewById(R.id.textview2);
ImageView imageview = view.findViewById(R.id.imageview);
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
int width = metric.widthPixels; // 屏幕宽度(像素)
final int height = metric.heightPixels; // 屏幕高度(像素)
ImageUtils.layoutView(view, width, height);
Bitmap bitmap = ImageUtils.viewSaveToBitmap(textView, button, textView2, imageview);
}}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.wenxin.test.MainActivity">
<TextView
android:id="@+id/textview1"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="@drawable/bg" />
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#ffffff" />
<TextView
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="@drawable/bg" />
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#ffffff" />
<LinearLayout
android:id="@+id/textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="@drawable/bg" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#ffffff" />
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="@drawable/bg" />
</LinearLayout>
public class ImageUtils {
//然后View和其内部的子View都具有了实际大小,也就是完成了布局,相当与添加到了界面上。接着就可以创建位图并在上面绘制了:
public static void layoutView(View v, int width, int height) {
// 整个View的大小 参数是左上角 和右下角的坐标
int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.AT_MOST);
/** 当然,measure完后,并不会实际改变View的尺寸,需要调用View.layout方法去进行布局。
* 按示例调用layout函数后,View的大小将会变成你想要设置成的大小。
*/
v.measure(measuredWidth, measuredHeight);
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
}
/**
* @param view1
* @param view2
* @param view3
* @param view4
* @return
*/
public static Bitmap viewSaveToBitmap(View view1, View view2, View view3, View view4) {
/**
* View组件显示的内容可以通过cache机制保存为bitmap
* 我们要获取它的cache先要通过setDrawingCacheEnable方法把cache开启,
* 然后再调用getDrawingCache方法就可 以获得view的cache图片了
* 。buildDrawingCache方法可以不用调用,因为调用getDrawingCache方法时,
* 若果 cache没有建立,系统会自动调用buildDrawingCache方法生成cache。
* 若果要更新cache, 必须要调用destoryDrawingCache方法把旧的cache销毁,才能建立新的。
*/
// 把一个View转换成图片
Bitmap view1Bmp = loadBitmapFromView(view1);
Bitmap view2Bmp = loadBitmapFromView(view2);
Bitmap view3Bmp = loadBitmapFromView(view3);
Bitmap view4Bmp = loadBitmapFromView(view4);
Bitmap bitmap = toConformBitmap(view1Bmp, view2Bmp, view3Bmp, view4Bmp);
// 得到新的图片
view1.destroyDrawingCache();
view2.destroyDrawingCache();
view3.destroyDrawingCache();
view4.destroyDrawingCache();
return bitmap;
}
/**
* 根据view生成图片
*
* @param view
* @return
*/
public static Bitmap loadBitmapFromView(View view) {
final boolean drawingCacheEnabled = true;
view.setDrawingCacheEnabled(drawingCacheEnabled);
view.buildDrawingCache(drawingCacheEnabled);
Bitmap drawingCache = view.getDrawingCache();
if (drawingCache != null) {
Bitmap.Config cfg = drawingCache.getConfig();
Log.d("测试", "cache.getConfig() = " + cfg);
}
Bitmap bitmap;
if (drawingCache != null) {
bitmap = Bitmap.createBitmap(drawingCache);
view.setDrawingCacheEnabled(false);
} else {
bitmap = null;
}
return bitmap;
}
/**
* 合并图片
*
* @param head 图片头
* @param bitmap 图片
* @return 新图片
*/
public static Bitmap toConformBitmap(Bitmap head, Bitmap bitmap, Bitmap bitmap1, Bitmap foot) {
if (head == null || bitmap == null) {
return null;
}
int bitmapWidth = bitmap.getWidth();
int headHeight = head.getHeight();
int bitmapHeight = bitmap.getHeight();
int footHeight = foot.getHeight();
int bitmap1Height = bitmap1.getHeight();
//生成四个图片合并大小的Bitmap
Bitmap newbmp = Bitmap.createBitmap(bitmapWidth, headHeight + bitmapHeight + bitmap1Height + footHeight, bitmap.getConfig());
Canvas cv = new Canvas(newbmp);
cv.drawBitmap(head, 0, 0, null);// 在 0,0坐标开始画入headBitmap
cv.drawBitmap(bitmap, 0, headHeight, null);
cv.drawBitmap(bitmap1, 0, headHeight + bitmapHeight, null);
cv.drawBitmap(foot, 0, headHeight + bitmapHeight + bitmap1Height, null);
//回收
head.recycle();
bitmap.recycle();
bitmap1.recycle();
foot.recycle();
return newbmp;
} }
打印的日志是:
11-05 17:57:33.682 4261-4261/? D/测试: cache.getConfig() = ARGB_565
11-05 17:57:33.694 4261-4261/? D/测试: cache.getConfig() = ARGB_565
11-05 17:57:33.725 4261-4261/? D/测试: cache.getConfig() = ARGB_8888
11-05 17:57:33.736 4261-4261/? D/测试: cache.getConfig() = ARGB_565











网友评论