Android图片画廊式倒影显示

package cn.gallery;

 import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

//本案例使用自己定义的图片资源来显示,其他功能如:打开画廊时,自动扫描SDCard中的图片资源,再将其显示,或者定义一个按钮,添加一个按钮事件,手动添加自己所需要的图片

public class MainActivity extends Activity {
 //资源类
    private Resources r;

    //定义原图和倒影之间的间隙
    int gap = 4 ;
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        r = this.getResources();
        MyGallery g = (MyGallery) findViewById(R.id.gallery);
        MyAdapter a = new MyAdapter();
        //画大图
        a.createBigBmps();
        g.setAdapter(a);
    }
   
    /**
     * 自定义适配器
     */
    class MyAdapter extends BaseAdapter{
     int[] resids = {
       R.drawable.img0001,
       R.drawable.img0030,
       R.drawable.img0100,
       R.drawable.img0130,
       R.drawable.img0200,
       R.drawable.img0230,
       R.drawable.img0300,
       R.drawable.img0330,
       R.drawable.img0354
     };
     
     ImageView[] ivs ;
     
  public int getCount() {
   return resids.length;
  }

  /**
   * 画大图
   */
  public void createBigBmps() {
   ivs = new ImageView[resids.length];
   //遍历所有原图
   for(int i = 0 ; i < resids.length ; i ++){
    //解码原始图片
    Bitmap srcBmp = BitmapFactory.decodeResource(r, resids[i]);
    //取得原图的宽度和高度
    int srcWidth = srcBmp.getWidth();
    int srcHeight = srcBmp.getHeight();
    
    //创建大图
    Bitmap bigBmp = Bitmap.createBitmap(srcWidth, srcHeight + (srcHeight/2) + gap, Bitmap.Config.ARGB_8888);
    
    //实例化画板
    Canvas canvas = new Canvas(bigBmp);
    
    /********************* 画原始图像*****************************/
    canvas.drawBitmap(srcBmp, 0, 0, null);
    
    /********************* 画间隙*****************************/
    Paint paint = new Paint();
    canvas.drawRect(0, srcHeight, srcWidth, srcHeight + gap, paint);
    
    /********************* 画倒影*****************************/
    //先从原图中截取下班部分
    Matrix matrix = new Matrix();
    //-1:方向相反,大小不变
    matrix.setScale(1, -1);
    //创建倒影图片
    Bitmap shaddowBmp = Bitmap.createBitmap(srcBmp, 0, srcHeight / 2, srcWidth, srcHeight / 2, matrix, false);
    //将倒影绘制到大图上去
    canvas.drawBitmap(shaddowBmp, 0, srcHeight + gap, null);
    
    /********************* 画倒影*****************************/
    paint = new Paint();
    
    //线性着色器(梯度着色).
    LinearGradient shader = new LinearGradient(0, srcHeight + gap, 0, srcHeight + gap + (srcHeight/2), 0x70ffffff, 0x00ffffff, Shader.TileMode.CLAMP);
    //设置着色器
    paint.setShader(shader);
    //设置传输模式
    PorterDuffXfermode mode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
    paint.setXfermode(mode);
    
    canvas.drawRect(0, srcHeight + gap, srcWidth, srcHeight + (srcHeight/2) + gap, paint);
    
    //创建ImageView控件,分别设置bigBmp.
    ImageView iv = new ImageView(MainActivity.this);
    Gallery.LayoutParams params = new Gallery.LayoutParams(180,240);
    iv.setLayoutParams(params);
    iv.setImageBitmap(bigBmp);
    ivs[i] = iv ;
   }
  }

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

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

  public View getView(int position, View convertView, ViewGroup parent) {
   return ivs[position];
  }
    }
   
   
}

 

package cn.mygallery;

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;

/**
 * 自定义画廊
 */
public class MyGallery extends Gallery {
 //画廊x中心点
 public int xGalleryCenter ;
 //画廊y中心点
 public int yGalleryCenter ;

 //最大旋转角
 int maxAngle = 60 ;
 
 //最大
 int maxZoom = -120 ;
 
 public MyGallery(Context context) {
  super(context);
  //设置允许孩子进行静态变换,使得ViewGroup支持Child静态变换,每次绘制child时,
  //会调用getChildStaticTransformation()方法
  setStaticTransformationsEnabled(true);
 }
 
 public MyGallery(Context context,AttributeSet set) {
  super(context,set);
  setStaticTransformationsEnabled(true);
 }
 
 /**
  * 取得孩子控件的静态变换对象
  */
 protected boolean getChildStaticTransformation(View child, Transformation t) {
  //将变换清除,并设置矩阵变换
  t.clear();
  t.setTransformationType(Transformation.TYPE_MATRIX);

  //得到孩子的宽度
  int childWidth = child.getWidth();
  //取得child的中心点坐标
  int xChildCenter = (child.getWidth() / 2) + child.getLeft();
  
  //判断view是否在画廊中心点上
  if((xChildCenter == xGalleryCenter)){
   //0:旋转角
   transformationImageView(child,t,0);
  }
  //不在中心点,需要进行旋转
  else{
   //应该做的旋转角度
   int angle = (int)(((float)(xGalleryCenter - xChildCenter) / childWidth) * maxAngle) ;
   //取得旋转角绝对值,并计算旋转角
   int absAngle = Math.abs(angle);
   if(absAngle > maxAngle){
    angle = angle > 0 ? maxAngle : -maxAngle ;
   }
   transformationImageView(child,t,angle);
  }
  return true;
 }
 
 /**
  * 变换控件
  */
 private void transformationImageView(View child, Transformation t, int angle) {
  int childWidth = child.getWidth();
  int childHeight = child.getHeight();
  //相机
  Camera canvas = new Camera();
  canvas.save();
  canvas.translate(0, 0, 100);
  
  int absAngle = Math.abs(angle);
  
  //放大量
  int zoom =0 ;
  //
  if(absAngle < maxAngle){
   zoom = (int)(absAngle * 1.5) + maxZoom ;
   //将child进行放大
   canvas.translate(0, 0, zoom);
  }
  //绕y轴旋转
  canvas.rotateY(angle);
  
  Matrix matrix = t.getMatrix() ;
  canvas.getMatrix(matrix);
  
  //平移预处理
  matrix.preTranslate(-(childWidth/2), -(childHeight/2));
  //平移后处理
  matrix.postTranslate((childWidth/2), (childHeight/2));
  canvas.restore();
 }

 /**
  *
  */
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  super.onSizeChanged(w, h, oldw, oldh);
  this.xGalleryCenter = ((w - this.getPaddingLeft() - this.getPaddingRight()) / 2 ) + this.getPaddingLeft() ;
  this.yGalleryCenter = ((h - this.getPaddingTop() - this.getPaddingBottom()) / 2 ) + this.getPaddingTop() ;
 }
}

 

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭