Android 零碎知识点

本文介绍了Android开发中常见的UI调整方法,包括解决EditText自动弹出软键盘的问题、实现图片点击放大功能、正确引用变量避免数据错误、获取资源图片保持原始尺寸的方法以及调整ImageView显示以避免被图片撑大的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.EditText自动弹出软键盘及软键盘遮挡EditText:

清单文件里设置activity属性:

android:windowSoftInputMode="adjustPan"

2.点击图片放大效果实现:

第一步将ImageView里的图片传到Activity:

Intent intent = new Intent(ResultActivity.this, ImageActivity.class);
intent.putExtra("image", Bitmap2Bytes(Bitmap.createBitmap(image.getDrawingCache())));
startActivity(intent);

private byte[] Bitmap2Bytes(Bitmap bm){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 80, baos);
    return baos.toByteArray();
}

Activity里显示图片:

Intent intent = getIntent();
byte buff[] = intent.getByteArrayExtra("image");
iv.setImageBitmap(BitmapFactory.decodeByteArray(buff, 0, buff.length));

注意:使用image.getDrawingCache()前需设置:image.setDrawingCacheEnabled(true);使用之后不要设置image.setDrawingCacheEnabled(false),否则再次放大图片会报错。

第二步重新获取图片并显示。因为此时Activity显示的是与ImageView里一般大小的图片。

3.变量的引用:

for(int i=0;i<jsonArr.length();i++){
    ProductBean productBean = new ProductBean();
    productBean.setProductCode(productCode);
    productBean.setProductName(productName);
    temp_list.add(productBean);
}

ProductBean productBean = new ProductBean();不可以放到for循环外面,否则会导致temp_list所有的数据都变成productBean最后一次set的值。


4.获取drawable里的图片并保持原始尺寸:

private Bitmap decodeResource(Resources resources, int id) {
    TypedValue value = new TypedValue();
    resources.openRawResource(id, value);
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inTargetDensity = value.density;
    return BitmapFactory.decodeResource(resources, id, opts);
}


5.ImageView被图片撑大:

设置图片宽为match_parent,高为wrap_content,此时如果图片原始尺寸宽度大于parent,那么ImageView的高会被撑大,这时需设置:

android:adjustViewBounds="true"





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值