您现在的位置:首页 > 博客 > 源码下载 > 正文
Android 3D倒影相册开发
http://www.drovik.com/      2012-11-7 11:26:52      来源:CSDN社区      点击:

 

先上效果图:

 

 

 

 部分代码:

 

public class MainActivity extends Activity implements OnItemSelectedListener{

private CustomGallery gallery;
private ImageAdapter imageAdapter;

private int[] resIds = new int[] { R.drawable.item1, R.drawable.item2,
R.drawable.item3, R.drawable.item4, R.drawable.item5,
R.drawable.item6, R.drawable.item7, R.drawable.item8,
R.drawable.item9, R.drawable.item10, R.drawable.item11,
R.drawable.item12, R.drawable.item13, R.drawable.item14,
R.drawable.item15 };

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery = (CustomGallery) findViewById(R.id.gallery);
gallery.setSelection(2, true);
gallery.setAnimationDuration(1000);
imageAdapter = new ImageAdapter(this);
gallery.setAdapter(imageAdapter);
gallery.setOnItemSelectedListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// 选中Gallery中某个图像时,在ImageSwitcher组件中放大显示该图像

}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}
public class ImageAdapter extends BaseAdapter {

int mGalleryItemBackground;

private Context mContext;

public ImageAdapter(Context context) {
mContext = context;
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground = typedArray.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, 0);
typedArray.recycle();
}

// 第1点改进,返回一个很大的值,例如,Integer.MAX_VALUE
public int getCount() {
return Integer.MAX_VALUE;
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = createReflectedImages(mContext, resIds[position%resIds.length]);
i.setLayoutParams(new CustomGallery.LayoutParams(120, 100));
i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
// 设置的抗锯齿,防止图像在旋转的时候出现锯齿
BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
drawable.setAntiAlias(true);
return i;
}
}

/**
 * 为图片添加倒影
 * @param mContext
 * @param imageId
 * @return
 */
public ImageView createReflectedImages(Context mContext, int imageId) {
Bitmap originalImage = BitmapFactory.decodeResource(
mContext.getResources(), imageId);
final int reflectionGap = 4;
int width = originalImage.getWidth();
int height = originalImage.getHeight();

Matrix matrix = new Matrix();
matrix.preScale(1, -1);

Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
height / 2, width, height / 2, matrix, false);

Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
(height + height / 2), Config.ARGB_8888);

Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(originalImage, 0, 0, null);

Paint deafaultPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0,
originalImage.getHeight(), 0, bitmapWithReflection.getHeight()
+ reflectionGap, 0x70ffffff, 0x00ffffff,
TileMode.MIRROR);

paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);

ImageView imageView = new ImageView(mContext);
imageView.setImageBitmap(bitmapWithReflection);

return imageView;
}

}

 

工程代码下载地址:

http://www.drovik.com/android/demo/3DGalleryDemo.rar

 

 

 

分享到:
发表评论(3)
1楼 cartier watch replicas  发表于  2015-3-30 20:09:41
Nice post. I’ve learned more from your aticle.Thanks cartier watch replicas http://jiayouyumaoqiu.com/bbs/forum.php?mod=viewthread&tid=40733&extra=
2楼 ルイヴィトン財布お直し  发表于  2015-7-8 4:23:44
ルイヴィトン財布お直し http://www.sinopromotion.com/bottoms.asp?qb80-xq3me.htm, ルイヴィトンバッグエヴァ http://www.sinopromotion.com/bottoms.asp?hq62-fv7it.htm, ルイヴィトン財布アンプラント http://www.sinopromotion.com/bottoms.asp?by36-hg2ac.htm, ルイヴィトンダミエジェアンバッグ http://www.sinopromotion.com/bottoms.asp?ia66-ey4ch.htm, ルイヴィトン財布中古品 http://www.sinopromotion.com/bottoms.asp?ai94-cl4ri.htm,
3楼 www.successmens.com  发表于  2015-9-16 1:15:34
2010 Shoes Trend www.successmens.com http://www.successmens.com/
姓名 *
评论内容 *
验证码 *图片看不清?点击重新得到验证码