指定在对象的X和Y轴上如何放置内容。指定一下常量中的一个或多个(使用 “|”分割)
属性名称 |
描述 |
|||||||||||||||||||||||||||||||||||||||
android:animationDuration |
设置布局变化时动画的转换所需的时间(毫秒级)。仅在动画开始时计时。该值必须是整数,比如:100。 |
|||||||||||||||||||||||||||||||||||||||
android:gravity |
指定在对象的X和Y轴上如何放置内容。指定一下常量中的一个或多个(使用 “|”分割)
|
|||||||||||||||||||||||||||||||||||||||
android:spacing |
(译者注:设置图片之间的间距) |
|||||||||||||||||||||||||||||||||||||||
android:unselectedAlpha |
设置未选中的条目的透明度(Alpha)。该值必须是float类型,比如:“1.2”。 |
ImagesAdapter适配器Gallery展示,注意如何让图片循环显示,循环到最后一张时自动跳转到第一张开始循环
package com.ncsyeyy.YeyyGallery;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
/**
* Created by yeyy on 9/15/2015.
*/
public class ImagesAdapter extends BaseAdapter {
private Context context;
private Integer[] imgsArray;
public ImagesAdapter() {
}
public ImagesAdapter(Context context, Integer[] imgsArray) {
this.context = context;
this.imgsArray = imgsArray;
}
@Override
public int getCount() {
// 返回数组的总数
// return imgsArray.length;
return Integer.MAX_VALUE;//循环到最后一张时,自己跳到第一张
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null){
//返回的是ImageView,因为你要实现的是相册
ImageView view=new ImageView(this.context);
// 得到图片资源id
// int id=imgsArray[position];
int id=imgsArray[position%imgsArray.length];//当循环到最后一张时自动跳转到第一张
// 设置imageview的资源id
view.setImageResource(id);
// 对ImageView进行布局
view.setLayoutParams(new Gallery.LayoutParams(80,80));
// 设置ImageView的图片显示类型为fitxy
view.setScaleType(ImageView.ScaleType.FIT_XY);
convertView=view;
}
return convertView;
}
}
package com.ncsyeyy.YeyyGallery;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageView;
public class MyActivity extends Activity {
private Gallery gallery;
private ImageView iv;
private Button btn;
private Integer[] imgsIds={
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d,
R.drawable.e,
R.drawable.f
};
private ImagesAdapter adapter;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();
setGallery();
}
private void setGallery(){
adapter = new ImagesAdapter(this,imgsIds);
gallery.setAdapter(adapter);
gallery.setOnItemClickListener(myOnItemListener);
}
// 自定义OnItemClickListener监听
AdapterView.OnItemClickListener myOnItemListener=new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
// 设置imageview的图片资源
iv.setImageResource(imgsIds[position]);
}
};
private void findView(){
gallery = (Gallery) findViewById(R.id.gallery);
iv = (ImageView) findViewById(R.id.iv);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MyActivity.this, GalleryActivity.class);
startActivity(intent);
}
});
}
}
3D Gallery显示效果:
package com.ncsyeyy.YeyyGallery;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;
import android.widget.ImageView;
/**
* Created by yeyy on 9/16/2015.
*/
public class MyCoverFlow extends Gallery {
// 定义Camera对象来实现Gallery的伪3D效果
private Camera camera = new Camera();
// 表姐Gallery旋转的最大角度
private int maxRotation = 60;
// 当旋转角度改变是图片大小进行改变
private int zoom = -120;
// 定义CoverFlow的中心点
private int center;
// 构造方法
public MyCoverFlow(Context context) {
super(context);
// 设置图片可有倒影效果
this.setStaticTransformationsEnabled(true);
}
public MyCoverFlow(Context context, AttributeSet attrs) {
super(context, attrs);
this.setStaticTransformationsEnabled(true);
}
public MyCoverFlow(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.setStaticTransformationsEnabled(true);
}
//得到图片的中心位置
public int getCenter() {
return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();
}
// 得到整个View的中心位置
private int getCenterOfView(View view) {
return view.getLeft() + view.getWidth() / 2;
}
protected boolean getChildStaticTransformation(View child, Transformation trans) {
// 得到中心点
final int childCenter = getCenterOfView(child);
// 得到item的宽度
final int childWidth = child.getWidth();
// 默认角度为0
int rotationAngle = 0;
trans.clear();
// 根据Matrix矩阵旋转
trans.setTransformationType(Transformation.TYPE_MATRIX);
if (childCenter == center) {
// 设置旋转后的图片
setTransImage((ImageView) child, trans, 0);
} else {
rotationAngle = (int) (((float) (center - childCenter) / childWidth) * maxRotation);
if (Math.abs(rotationAngle) > maxRotation) {
rotationAngle = (rotationAngle < 0) ? -maxRotation : maxRotation;
}
// 设置旋转后的图片
setTransImage((ImageView) child, trans, rotationAngle);
}
return true;
}
// 当Gallery的大小改变时回调
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
center = getCenter();
super.onSizeChanged(w, h, oldw, oldh);
}
private void setTransImage(ImageView child, Transformation t, int rotationAngle) {
// 保存当前内容
camera.save();
// 得到旋转矩阵
final Matrix imageMatrix = t.getMatrix();
// 得到imageview的高度
final int imageHeight = child.getLayoutParams().height;
// 得到imageview的宽度
final int imageWhith = child.getLayoutParams().width;
// 得到imageview的角度
final int rotation = Math.abs(rotationAngle);
// 根据camer的移动方向决定角度
camera.translate(0.0f, 0.0f, 100.0f);
// 根据旋转的角度得到图片的放大倍数
if (rotation < maxRotation) {
float zoomAmount = (float) (zoom + (rotation * 1.5));
camera.translate(0.0f, 0.0f, zoomAmount);
}
// 设置y轴的旋转角度
camera.rotateY(rotationAngle);
// 得到旋转矩阵
camera.getMatrix(imageMatrix);
// 设置旋转矩阵的倒影宽高
imageMatrix.preTranslate(-(imageWhith / 2), -(imageHeight / 2));
imageMatrix.preTranslate((imageWhith / 2), (imageHeight / 2));
// 保存视角效果
camera.restore();
}
}
package com.ncsyeyy.YeyyGallery;
import android.content.Context;
import android.graphics.*;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
/**
* Created by yeyy on 9/16/2015.
*/
public class ImgsAdapter extends BaseAdapter {
// 得到上下文环境
private Context context;
//设置图片的id
private Integer[] ImagIds;
// 保存最终需要绘制的imageview数组
private ImageView[] Images;
public ImgsAdapter(Context c, Integer[] imagIds) {
context = c;
ImagIds = imagIds;
Images =new ImageView[ImagIds.length];
}
public ImgsAdapter() {
}
//旋转图片
public boolean invertedImages(){
// 倒影图片的高度
final int reflectionGap=4;
int index=0;
// 遍历每个imageview,生成倒影
for (int imageId:ImagIds){
Bitmap originalImage= BitmapFactory.decodeResource(context.getResources(),imageId);
// 得到图片的高度
int width=originalImage.getWidth();
int height=originalImage.getHeight();
// 通过matrix矩阵生成图片的旋转图片
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),
Bitmap.Config.ARGB_8888);
// 通过canvas合并图片和图片的倒影
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);
// 用LinearGradient画笔绘制倒影图片
Paint paint=new Paint();
LinearGradient shader=new LinearGradient(0,originalImage.getHeight(),0,
bitmapWithReflection.getHeight()+reflectionGap,
0x70ffffff,0x00ffffff, Shader.TileMode.CLAMP);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.drawRect(0,height,width,bitmapWithReflection.getHeight()+reflectionGap,paint);
// 将绘制完的图片生成imageview
ImageView imageView=new ImageView(context);
imageView.setImageBitmap(bitmapWithReflection);
imageView.setLayoutParams(new Gallery.LayoutParams(100,200));
// 加入imageview数组
Images[index++]=imageView;
}
return true;
}
@Override
public int getCount() {
return ImagIds.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return Images[position];
}
}
package com.ncsyeyy.YeyyGallery;
import android.app.Activity;
import android.os.Bundle;
/**
* Created by yeyy on 9/16/2015.
*/
public class GalleryActivity extends Activity {
private Integer[] imgs={
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d,
R.drawable.e,
R.drawable.f
};
// 定义自定义的CoverFlow类的对象
private MyCoverFlow CoverFlow;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
setCoverFlow();
}
private void setCoverFlow(){
// 定义自定义的ImgsAdapter对象
ImgsAdapter adapter=new ImgsAdapter(this,imgs);
// 设置adapter的倒影图片
adapter.invertedImages();
// 得到MyCoverFlow对象设置起adapter
CoverFlow =(MyCoverFlow) findViewById(R.id.mcf);
CoverFlow.setAdapter(adapter);
}
}