这个适配器对于圆形图像视图非常有用,但圆形图像视图可以从右到左,不可能从左到右,所以请建议我在这段代码中应该写什么,这样图像可以从左边看到圆形对吧?
public class ImageAdapterCircleGallery extends BaseAdapter {
private Context mContext;
int itemBackground;
Integer[] mImageIds = { R.drawable.img1, R.drawable.img2,
R.drawable.img3, R.drawable.img4, R.drawable.img5,
R.drawable.img6, R.drawable.img7, R.drawable.img8,
R.drawable.img9, R.drawable.img10, R.drawable.img11,
R.drawable.img12, R.drawable.img13, R.drawable.img14,
R.drawable.img15, R.drawable.img16, R.drawable.img17,
R.drawable.img18, R.drawable.img19, R.drawable.img20,
R.drawable.img21, R.drawable.img22, R.drawable.img23,
R.drawable.img24, R.drawable.img25, R.drawable.img26,
R.drawable.img27, R.drawable.img28, R.drawable.img29,
R.drawable.img30 };
public ImageAdapterCircleGallery(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 1);
a.recycle();
}
public int getCount() {
return Integer.MAX_VALUE;
}
public Object getItem(int position) {
return getPosition(position);
}
public long getItemId(int position) {
return getPosition(position);
}
public View getView(int position, View convertView, ViewGroup parent) {
/*ImageView i = new ImageView(mContext);
position = getPosition(position);
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(100, 100));
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
return i;*/
ImageView imageView = new ImageView(mContext);
position = getPosition(position);
imageView.setImageResource(imageIDs[position]);
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(90, 90));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
public int checkPosition(int position) {
return getPosition(position);
}
int getPosition(int position) {
if (position >= mImageIds.length) {
position = position % mImageIds.length;
}
return position;
}
}
答案 0 :(得分:1)
将我的代码用于圆角ImageView。它可以确保无论您在其中设置什么图像,它都将完美呈现: -
package com.example.listviewwithimages;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class RoundedImageView extends ImageView {
public RoundedImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public RoundedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
if (drawable == null) {
return;
}
if (getWidth() == 0 || getHeight() == 0) {
return;
}
Bitmap b = ((BitmapDrawable)drawable).getBitmap() ;
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
int w = getWidth(), h = getHeight();
Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
canvas.drawBitmap(roundBitmap, 0,0, null);
}
public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
Bitmap sbmp;
if(bmp.getWidth() != radius || bmp.getHeight() != radius)
sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
else
sbmp = bmp;
Bitmap output = Bitmap.createBitmap(sbmp.getWidth(),
sbmp.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xffa19774;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
// canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f,
// sbmp.getWidth() / 2+0.1f, paint);
canvas.drawCircle(sbmp.getWidth()/2, sbmp.getHeight()/2,
(sbmp.getWidth()/2), paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(sbmp, rect, rect, paint);
return output;
}
}
并在你的xml中,像这样使用它: -
<com.example.listviewwithimages.RoundedImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="Big Image" />
适配器中的,在getView()
中 RoundedImageView imageView = new RoundedImageView(mContext);