倒影效果(源码)

package yy.android.ImageR;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
import android.widget.Toast;

public class ImageRActivity extends Activity {

    private TextView tvTitle;     
    private myGallery gallery;     
    private ImageAdapter adapter;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initRes();
    }
    private void initRes(){
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        gallery = (myGallery) findViewById(R.id.mygallery);

        adapter = new ImageAdapter(this);     
        adapter.createReflectedImages();//创建倒影效果
        gallery.setAdapter(adapter);//给gallery 设置适配器
        //选中事件监听器
        gallery.setOnItemSelectedListener(new OnItemSelectedListener() {    
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                tvTitle.setText(adapter.titles[position]);
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        //点击事件监听器
        gallery.setOnItemClickListener(new OnItemClickListener() {          
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(ImageRActivity.this, "img " + (position+1) + " selected", Toast.LENGTH_SHORT).show();
            }
        });
    }

}

///

package yy.android.ImageR;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class ImageAdapter extends BaseAdapter {
    private ImageView[] mImages;//子图

    private Context mContext;
    public List<Map<String, Object>> list;
    

    public Integer[] imgs = { R.drawable.img01, R.drawable.img02, R.drawable.img03,
                              R.drawable.img04, R.drawable.img05, R.drawable.img06, R.drawable.img07 };
    public String[] titles = {"1","2","3","4","5","6","7"};

    public ImageAdapter(Context c) {
        this.mContext = c;
        list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < imgs.length; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("image", imgs[i]);
            list.add(map);
        }
        mImages = new ImageView[list.size()];
    }

    /** 创建倒影效果 */
    public boolean createReflectedImages() {
        final int reflectionGap = 4;
        int index = 0;
        for (Map<String, Object> map : list) {
            Integer id = (Integer) map.get("image");
             // 获取原始图片
            Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), id);    
            int width = originalImage.getWidth();
            int height = originalImage.getHeight();
            //这个matrix的作用只是翻转原图,给左倒影用
            Matrix matrix = new Matrix();//新建一个转换矩阵
            matrix.preScale(1,  -1);    // 图片矩阵变换(从低部向顶部的倒影)  ,-1表示反方向
            // 截取原图下半部分  
            Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height/2, width, height/2, matrix, false);
            // 创建倒影图片(高度为原图3/2)
            Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);
            // 绘制倒影图(原图 + 间距 + 倒影)  注意这里只是创建一个用来装倒影图的画布
            Canvas canvas = new Canvas(bitmapWithReflection);    
            // 绘制原图  
            canvas.drawBitmap(originalImage, 0, 0, null);
            //这画笔的作用是绘制原图和倒影之间的间隔矩形
            Paint paint = new Paint();
            // 绘制原图与倒影的间距  
            canvas.drawRect(0, height, width, height + reflectionGap, paint);    
            // 绘制倒影图  
            canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);    
 
            //这支画笔的作用是再已有图上绘制阴影效果
            paint = new Paint();
            //阴影效果
            LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
            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);        
            imageView.setLayoutParams(new myGallery.LayoutParams(180,240));
            imageView.setScaleType(ScaleType.MATRIX);
            mImages[index++] = imageView;
        }
        return true;
    }

    public int getCount() {
        return imgs.length;
    }

    public Object getItem(int position) {
        return mImages[position];
    }

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

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

    public float getScale(boolean focused, int offset) {
        return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
    }
}
/

package yy.android.ImageR;

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;

public class myGallery extends Gallery {

    private Camera mCamera = new Camera();
    private int mMaxRotationAngle = 60;    // 最大旋转角度 60  
    private int mMaxZoom = -120;
    private int mCoveflowCenter;//Gallery的中心x

    public myGallery(Context context) {
        super(context);
        this.setStaticTransformationsEnabled(true);//设置为true时,允许多子类进行静态转换
        //也就是说把这个属性设成true的时候每次viewGroup(看Gallery的源码就可以看到它是从ViewGroup间
        //接继承过来的)在重新画它的child的时候都会促发getChildStaticTransformation这个函数,所以我
        //们只需要在这个函数里面去加上旋转和放大的操作就可以了
    }
 
    public myGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setStaticTransformationsEnabled(true);
    }

    public myGallery(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.setStaticTransformationsEnabled(true);
    }

    public int getMaxRotationAngle() {
        return mMaxRotationAngle;//获取最大转角
    }

    public void setMaxRotationAngle(int maxRotationAngle) {
        mMaxRotationAngle = maxRotationAngle;//设置最大转角
    }

    public int getMaxZoom() {
        return mMaxZoom;//最大放大值
    }

    public void setMaxZoom(int maxZoom) {
        mMaxZoom = maxZoom;//设置最大放大值
    }
     /** 获取Gallery的中心x */   
    private int getCenterOfCoverflow() {
        return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();
    }

      /** 获取View的中心x */
    private static int getCenterOfView(View view) {
        return view.getLeft() + view.getWidth() / 2;
    }

    @Override//尺寸改变后,重新获得gallery中心点
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mCoveflowCenter = getCenterOfCoverflow();//重新获得Gallery的中心点
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected boolean getChildStaticTransformation(View child, Transformation trans) {
        final int childCenter = getCenterOfView(child);//获得View的中点
        final int childWidth = child.getWidth();//获得view的宽
        int rotationAngle = 0;//旋转角度

        trans.clear();//重新设置
        // alpha 和 matrix 都变换   
        trans.setTransformationType(Transformation.TYPE_BOTH);        
        //正中间的childview
        if (childCenter == mCoveflowCenter) {//如果View正中间等于Gallery正中间的话
            transformImageBitmap((ImageView) child, trans, 0);//正中间图片旋转0度    
        } else {    // 两侧的childView ,它们的倾斜角度按比例旋转
            rotationAngle = (int) ( ( (float) (mCoveflowCenter - childCenter) / childWidth ) * mMaxRotationAngle );
            //限制最大角度
            if (Math.abs(rotationAngle) > mMaxRotationAngle) {
                rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;
            }
            transformImageBitmap((ImageView) child, trans, rotationAngle);
        }

        return true;
    }
    //负责做图片旋转的
    private void transformImageBitmap(ImageView child, Transformation trans, int rotationAngle) {
        mCamera.save();
        
        final Matrix imageMatrix = trans.getMatrix();//图片转换时需要用到的Matrix
        final int imageHeight = child.getLayoutParams().height;
        final int imageWidth = child.getLayoutParams().width;
        final int rotation = Math.abs(rotationAngle);//返回iujiaodu的绝对值

        // 在Z轴上正向移动camera的视角,实际效果为放大图片; 如果在Y轴上移动,则图片上下移动; X轴上对应图片左右移动。
        mCamera.translate(0.0f, 0.0f, 100.0f);

        // 随着角度的减小而放大
        if (rotation < mMaxRotationAngle) {//注意mMaxZoom是负的
            float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));
            mCamera.translate(0.0f, 0.0f, zoomAmount);
        }
        // rotationAngle 为正,沿y轴向内旋转; 为负,沿y轴向外旋转   
        mCamera.rotateY(rotationAngle);        
        
        mCamera.getMatrix(imageMatrix);//把Camera的动作矩阵传到imageMatrix
        imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
        imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
        mCamera.restore();
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="16sp" />
    
    <yy.android.ImageR.myGallery
        android:id="@+id/mygallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tvTitle"
        android:layout_marginTop="10dip" />

</RelativeLayout>

//


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现图片的倒影效果,可以使用CSS中的 transform 和 gradient 属性。 首先,在 HTML 中添加一个包含图片的容器,例如: ```html <div class="image-container"> <img src="your-image-url" alt="your-image"> </div> ``` 然后,在 CSS 中设置容器的样式,包括宽度、高度、相对定位和背景颜色: ```css .image-container { position: relative; width: 300px; height: 300px; background-color: #f2f2f2; } ``` 接着,使用伪元素 `::before` 创建一个半透明的黑色遮罩层,并通过 transform 属性将其翻转: ```css .image-container::before { content: ""; position: absolute; top: 100%; left: 0; width: 100%; height: 100%; background: linear-gradient(to bottom, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.8)); transform: scaleY(-1); transform-origin: bottom; } ``` 这里使用了渐变色来实现遮罩层的透明度渐变,从上到下逐渐变为全透明。同时,通过 transform: scaleY(-1) 和 transform-origin: bottom 来将遮罩层垂直翻转,并以底部为轴心进行翻转。 最后,将图片相对于容器进行绝对定位,并设置 z-index 属性使其在遮罩层之上: ```css .image-container img { position: absolute; top: 0; left: 0; z-index: 1; } ``` 完成以上步骤后,就可以实现图片的倒影效果了。完整代码如下: ```html <div class="image-container"> <img src="your-image-url" alt="your-image"> </div> ``` ```css .image-container { position: relative; width: 300px; height: 300px; background-color: #f2f2f2; } .image-container::before { content: ""; position: absolute; top: 100%; left: 0; width: 100%; height: 100%; background: linear-gradient(to bottom, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.8)); transform: scaleY(-1); transform-origin: bottom; } .image-container img { position: absolute; top: 0; left: 0; z-index: 1; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值