gridview 中的 setImageBitmap 不会缩放图像以适合
setImageBitmap in gridview does not scale the image to fit
注意:这个问题只发生在gridview中,如果在单个imageview中设置imagebitmap,效果很好。
看下面的图片,左图是我想要的,右图是我实际得到的。
唯一不同的是我设置图像的方式,如果我如下设置图像,我会得到正确的结果(上图左)。
holder.image.setImageResource(imagesArray.get(position));
但是如果我像下面这样用位图设置图像,图像没有按我想要的那样缩放(右上图)
holder.imageView.setImageBitmap(bitmap);
为什么我将图像设置为位图而不是可绘制的?
因为我的图像是加密的,所以我需要在加载到图像视图之前对其进行解密。基本步骤是:
- 将图片解密成byte arrya。
- 从字节创建位图。
- 将位图加载到图像视图中。
注意,问题不是解密引起的,我用没有解密的普通图片测试过,也是不行的。
这是代码
GridViewActivity
public class GridViewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gridview);
GridView gridView = (GridView)findViewById(R.id.grid_view);
ImageAdapter imageAdapter = ImageAdapter.getInstance();
imageAdapter.setContext(this);
gridView.setAdapter(imageAdapter);
for (int i = 0; i < 50; i++) {
int resourceId = getResources().getIdentifier("myimage", "drawable", getPackageName());
imageAdapter.imagesArray.add(resourceId);
}
}
}
ImageAdapter
public class ImageAdapter extends BaseAdapter {
private static ImageAdapter imageAdapter = null;
private Bitmap bitmap;
private Context mContext;
public ArrayList<Integer> imagesArray = new ArrayList<Integer>();
private ImageAdapter() {
}
public static ImageAdapter getInstance() {
if (imageAdapter == null) {
imageAdapter = new ImageAdapter();
}
return imageAdapter;
}
public void setContext(Context context) {
mContext = context;
}
@Override
public int getCount() {
return imagesArray.size();
}
@Override
public Object getItem(int position) {
return imagesArray.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
row = inflater.inflate(R.layout.row_grid, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView)row.findViewById(R.id.image);
row.setTag(holder);
}
else {
holder = (ViewHolder)row.getTag();
}
// Create bitmap from image in assets folder.
String imageFileName = "myimage.jpg";
AssetManager assetMgr = mContext.getAssets();
try {
InputStream fis = assetMgr.open(imageFileName);
bitmap = BitmapFactory.decodeStream(fis);
}
catch (Exception ex) {
Log.i("zdd", ex.getLocalizedMessage());
}
holder.imageView.setImageBitmap(bitmap);
//holder.image.setImageResource(imagesArray.get(position));
return row;
}
static class ViewHolder {
ImageView imageView;
}
}
SquareImageView
public class SquareImageView extends ImageView
{
public SquareImageView(Context context)
{
super(context);
}
public SquareImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}
}
布局如下
activity_gridview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#7f7f7f"
android:numColumns="4"
android:horizontalSpacing="2dp"
android:verticalSpacing="2dp"
android:gravity="center"
android:stretchMode="columnWidth"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
</GridView>
</RelativeLayout>
row_grid.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:background="#ffffff">
<com.jiyuzhai.setimagebitmaptest.SquareImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
</LinearLayout>
尝试将行项目的父大小更改为 "match_parent"。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
android:background="#ffffff">
<com.jiyuzhai.setimagebitmaptest.SquareImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
</LinearLayout>
尝试从线性布局中删除 "padding" 属性(或将其设置为 0dp)并在 SquareImageView 中将 scaleType 设置为 fitXY。
注意:这个问题只发生在gridview中,如果在单个imageview中设置imagebitmap,效果很好。
看下面的图片,左图是我想要的,右图是我实际得到的。
唯一不同的是我设置图像的方式,如果我如下设置图像,我会得到正确的结果(上图左)。
holder.image.setImageResource(imagesArray.get(position));
但是如果我像下面这样用位图设置图像,图像没有按我想要的那样缩放(右上图)
holder.imageView.setImageBitmap(bitmap);
为什么我将图像设置为位图而不是可绘制的? 因为我的图像是加密的,所以我需要在加载到图像视图之前对其进行解密。基本步骤是:
- 将图片解密成byte arrya。
- 从字节创建位图。
- 将位图加载到图像视图中。
注意,问题不是解密引起的,我用没有解密的普通图片测试过,也是不行的。
这是代码
GridViewActivity
public class GridViewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gridview);
GridView gridView = (GridView)findViewById(R.id.grid_view);
ImageAdapter imageAdapter = ImageAdapter.getInstance();
imageAdapter.setContext(this);
gridView.setAdapter(imageAdapter);
for (int i = 0; i < 50; i++) {
int resourceId = getResources().getIdentifier("myimage", "drawable", getPackageName());
imageAdapter.imagesArray.add(resourceId);
}
}
}
ImageAdapter
public class ImageAdapter extends BaseAdapter {
private static ImageAdapter imageAdapter = null;
private Bitmap bitmap;
private Context mContext;
public ArrayList<Integer> imagesArray = new ArrayList<Integer>();
private ImageAdapter() {
}
public static ImageAdapter getInstance() {
if (imageAdapter == null) {
imageAdapter = new ImageAdapter();
}
return imageAdapter;
}
public void setContext(Context context) {
mContext = context;
}
@Override
public int getCount() {
return imagesArray.size();
}
@Override
public Object getItem(int position) {
return imagesArray.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
row = inflater.inflate(R.layout.row_grid, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView)row.findViewById(R.id.image);
row.setTag(holder);
}
else {
holder = (ViewHolder)row.getTag();
}
// Create bitmap from image in assets folder.
String imageFileName = "myimage.jpg";
AssetManager assetMgr = mContext.getAssets();
try {
InputStream fis = assetMgr.open(imageFileName);
bitmap = BitmapFactory.decodeStream(fis);
}
catch (Exception ex) {
Log.i("zdd", ex.getLocalizedMessage());
}
holder.imageView.setImageBitmap(bitmap);
//holder.image.setImageResource(imagesArray.get(position));
return row;
}
static class ViewHolder {
ImageView imageView;
}
}
SquareImageView
public class SquareImageView extends ImageView
{
public SquareImageView(Context context)
{
super(context);
}
public SquareImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}
}
布局如下
activity_gridview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#7f7f7f"
android:numColumns="4"
android:horizontalSpacing="2dp"
android:verticalSpacing="2dp"
android:gravity="center"
android:stretchMode="columnWidth"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
</GridView>
</RelativeLayout>
row_grid.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:background="#ffffff">
<com.jiyuzhai.setimagebitmaptest.SquareImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
</LinearLayout>
尝试将行项目的父大小更改为 "match_parent"。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
android:background="#ffffff">
<com.jiyuzhai.setimagebitmaptest.SquareImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
</LinearLayout>
尝试从线性布局中删除 "padding" 属性(或将其设置为 0dp)并在 SquareImageView 中将 scaleType 设置为 fitXY。