安卓适配

来源:互联网 发布:淘宝网介绍ppt 编辑:程序博客网 时间:2024/06/14 07:22

1、表情包的适配:ImageSpan
(1)、ImageSpan是通过bitmap生成的,如果要调整图片的大小,你可以对bitmap进行预处理,比如scale之类的操作,生成你目标的bitmap再生成ImageSpan。

(2)、可以把图片设置到ImageView里,调整好ImageView里图片的大小,再把ImageView生成对应的bitmap,从而生成ImageSpan。

(3)、测试:设置bitmap的参数

// Bitmap的配置参数                BitmapFactory.Options opts = new BitmapFactory.Options();                opts.inDensity = context.getResources().getDisplayMetrics().densityDpi;                opts.inTargetDensity = context.getResources().getDisplayMetrics().densityDpi;                Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resId, opts);                // BitmapFactory.decodeResource(context.getResources(), resId);                // bitmap = Bitmap.createScaledBitmap(bitmap, 60, 60, true);                // 通过图片资源id来得到bitmap,用一个ImageSpan来包装                ImageSpan imageSpan = new ImageSpan(bitmap);                Drawable drawable = context.getResources().getDrawable(R.drawable.emoji_1);                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),                        drawable.getIntrinsicHeight());                ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);

(4)、matrix的类:操作bitmap的缩放:

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resId);                Matrix matrix = new Matrix();                matrix.setScale(0.5f, 0.5f);// 缩小为原来的一半                Bitmap bm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);                ImageSpan span = new ImageSpan(bm, ImageSpan.ALIGN_BASELINE);
0 0