80,471
社区成员




public class GalleryBugAdapter extends BaseAdapter{
private ArrayList<HashMap<String, String>> list;
private Context mContext;
private ImageView mImg;
public Bitmap bitmap;
private Gallery gallery;
private int tag = 0;
// rotate
private int nowAngle = 0, toAngle = 0;
private int w, h;
public GalleryBugAdapter(ArrayList<HashMap<String, String>> list, Context context, Gallery gallery) {
if (list != null) {
this.list = list;
} else {
this.list = new ArrayList<HashMap<String, String>>();
}
this.mContext = context;
this.gallery = gallery;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (tag != position) {
nowAngle = 0;
toAngle = 0;
}
tag = position;
mImg = new ImageView(mContext);
// 父控件的宽高
w = parent.getWidth();
h = parent.getHeight();
String path = list.get(position).get("list_img_path");
Bitmap bitmap = getBitmap(path, w, h);
mImg.setImageBitmap(bitmap);
mImg.setTag(position);
mImg.setFocusable(false);
mImg.setClickable(false);
mImg.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return mImg;
}
private Bitmap getBitmap(String imgPath, int nWidth, int nHeight) {
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
if (nWidth != 0 && nHeight != 0) {
options.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeFile(imgPath, options);
if (options.outWidth <= nWidth && options.outHeight <= nHeight) {
mImg.setScaleType(ScaleType.CENTER_INSIDE);
return BitmapFactory.decodeFile(imgPath);
}
int hRatio = (int) Math.ceil(options.outHeight / (double) nHeight);
int wRatio = (int) Math.ceil(options.outWidth / (double) nWidth);
options.inSampleSize = (hRatio > wRatio) ? hRatio : wRatio;
options.inJustDecodeBounds = false;
}
bitmap = BitmapFactory.decodeFile(imgPath, options);
return bitmap;
}
public void Rotate(boolean flag, int angle) {
mImg = (ImageView) gallery.findViewWithTag(tag);
AnimatorSet aSet = new AnimatorSet();
if (flag) {
toAngle = (toAngle + 90) % 360;
} else {
toAngle = (toAngle - 90) % 360;
}
if (angle == 1) {
toAngle = 0;
}
ObjectAnimator oAX = ObjectAnimator.ofFloat(mImg, "rotation", nowAngle, toAngle);
nowAngle = toAngle;
aSet.play(oAX);
aSet.setDuration(0);
aSet.start();
}
}
public void initGallery(int position, ArrayList<HashMap<String, String>> photoList) {
if (layoutGallery == null) {
layoutGallery = ((ViewStub) findViewById(R.id.photo_gallery)).inflate();
}
photoGallery = (Gallery) layoutGallery.findViewById(R.id.Gallery);
adapter = new GalleryAdapter(photoList, this, photoGallery);
photoGallery.setAdapter(adapter);
photoGallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int item, long id) {
Log.d("media", "dell-ItemSelected");
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
photoGallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("media", "dell-ItemClick");
}
});
btnRotate1 = (Button) findViewById(R.id.btnRotate1);
btnRotate2 = (Button) findViewById(R.id.btnRotate2);
btnRotate1.setOnClickListener(new OnClickListener() {// 逆时针旋转90度
@Override
public void onClick(View v) {
Log.e("PhotoRotate", "BUG-旋转后会让gallery的");
adapter.Rotate(false, 0);
}
});
btnRotate2.setOnClickListener(new OnClickListener() {// 顺时针旋转90度
@Override
public void onClick(View v) {
adapter.Rotate(true, 0);
}
});
}