Android編程滑動(dòng)效果之倒影效果實(shí)現(xiàn)方法(附demo源碼下載)
本文實(shí)例講述了Android編程滑動(dòng)效果之倒影效果實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
前面介紹了使用《Android編程實(shí)現(xiàn)3D滑動(dòng)旋轉(zhuǎn)效果的方法》,現(xiàn)在介紹圖片倒影實(shí)現(xiàn),先看效果圖
這里主要通過(guò)自定義Gallery和ImageAdapter(繼承自BaseAdapter)實(shí)現(xiàn)
1、倒影繪制
ImageAdapter繼承自BaseAdapter,詳細(xì)實(shí)現(xiàn)可見前面關(guān)于Android Gallery的用法。這里重點(diǎn)介紹倒影原理及實(shí)現(xiàn)
倒影原理:
倒影效果是主要由原圖+間距+倒影三部分組成,高度大約為原圖的3/2(原圖為1、倒影為1/2)
原圖,就是我們看到了最開始的圖片
間距,是原圖與倒影之間的間隙,如:reflectionGap = 4;
倒影,是原圖下半部分1/2高度,通過(guò)矩陣變換matrix.preScale(1, -1); 獲取倒立圖片,然后再加上線性遮罩和陰影實(shí)現(xiàn)
倒影實(shí)現(xiàn):
/** 反射倒影 */ 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 = 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); // 創(chuàng)建倒影圖片(高度為原圖3/2) 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); // 設(shè)置倒影圖片 imageView.setLayoutParams(new myGallery.LayoutParams(180, 240)); imageView.setScaleType(ScaleType.MATRIX); mImages[index++] = imageView; } return true; }
2、myGallery
自定義Gallery來(lái)實(shí)現(xiàn)倒影圖片的瀏覽與選擇
public class myGallery extends Gallery { private Camera mCamera = new Camera(); private int mMaxRotationAngle = 60; // 最大旋轉(zhuǎn)角度 60 private int mMaxZoom = -120; private int mCoveflowCenter; public myGallery(Context context) { super(context); this.setStaticTransformationsEnabled(true); } 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 protected void onSizeChanged(int w, int h, int oldw, int oldh) { mCoveflowCenter = getCenterOfCoverflow(); super.onSizeChanged(w, h, oldw, oldh); } @Override protected boolean getChildStaticTransformation(View child, Transformation trans) { final int childCenter = getCenterOfView(child); final int childWidth = child.getWidth(); int rotationAngle = 0; trans.clear(); trans.setTransformationType(Transformation.TYPE_BOTH); // alpha 和 matrix 都變換 if (childCenter == mCoveflowCenter) { // 正中間的childView transformImageBitmap((ImageView) child, trans, 0); } else { // 兩側(cè)的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(); final int imageHeight = child.getLayoutParams().height; final int imageWidth = child.getLayoutParams().width; final int rotation = Math.abs(rotationAngle); // 在Z軸上正向移動(dòng)camera的視角,實(shí)際效果為放大圖片; 如果在Y軸上移動(dòng),則圖片上下移動(dòng); X軸上對(duì)應(yīng)圖片左右移動(dòng)。 mCamera.translate(0.0f, 0.0f, 100.0f); // As the angle of the view gets less, zoom in if (rotation < mMaxRotationAngle) { float zoomAmount = (float) (mMaxZoom + (rotation * 1.5)); mCamera.translate(0.0f, 0.0f, zoomAmount); } mCamera.rotateY(rotationAngle); // rotationAngle 為正,沿y軸向內(nèi)旋轉(zhuǎn); 為負(fù),沿y軸向外旋轉(zhuǎn) mCamera.getMatrix(imageMatrix); imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2)); imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2)); mCamera.restore(); } }
3、Activity
Activity中,主要實(shí)現(xiàn)自定義Gallery的圖片填充ImageAdapter、myGallery選擇事件監(jiān)聽、點(diǎn)擊事件監(jiān)聽
private void initRes(){ tvTitle = (TextView) findViewById(R.id.tvTitle); gallery = (myGallery) findViewById(R.id.mygallery); // 獲取自定義的myGallery控件 adapter = new ImageAdapter(this); adapter.createReflectedImages(); // 創(chuàng)建倒影效果 gallery.setAdapter(adapter); gallery.setOnItemSelectedListener(new OnItemSelectedListener() { // 設(shè)置選擇事件監(jiān)聽 @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { tvTitle.setText(adapter.titles[position]); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); gallery.setOnItemClickListener(new OnItemClickListener() { // 設(shè)置點(diǎn)擊事件監(jiān)聽 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(Main.this, "img " + (position+1) + " selected", Toast.LENGTH_SHORT).show(); } }); }
main.xml布局文件中,通過(guò)實(shí)現(xiàn)自定義的myGallery,來(lái)顯示圖片集合
<?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" /> <com.homer.reflect.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>
完整實(shí)例代碼點(diǎn)擊此處本站下載。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)動(dòng)畫技巧匯總》、《Android開發(fā)入門與進(jìn)階教程》及《Android控件用法總結(jié)》。
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- android顯示TextView文字的倒影效果實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)動(dòng)態(tài)向Gallery中添加圖片及倒影與3D效果示例
- Android中使用Matrix控制圖形變換和制作倒影效果的方法
- Android應(yīng)用開發(fā)之簡(jiǎn)易、大氣音樂(lè)播放器實(shí)現(xiàn)專輯倒影效果
- Android 輕松實(shí)現(xiàn)圖片倒影效果實(shí)例代碼
- android Bitmap圓角與倒影的具體實(shí)現(xiàn)代碼
- Android 圖像處理(類型轉(zhuǎn)換,比例縮放,倒影,圓角)的小例子
- Android 倒影算法的實(shí)現(xiàn)代碼
- Android自定義TextView實(shí)現(xiàn)文字傾斜效果
- Android實(shí)現(xiàn)文字翻轉(zhuǎn)動(dòng)畫的效果
- Android實(shí)現(xiàn)文字和圖片混排(文字環(huán)繞圖片)效果
- Android編程實(shí)現(xiàn)文字倒影效果的方法
相關(guān)文章
Android大作業(yè)功能設(shè)計(jì)之自動(dòng)登錄和記住密碼
SharedPreferences是Android平臺(tái)上一個(gè)輕量級(jí)的存儲(chǔ)類,主要是保存一些常用的配置參數(shù),它是采用xml文件存放數(shù)據(jù)的,文件存放在"/data/data<package?name>/shared_prefs"目錄下,由于SharedPreferences是一個(gè)接口,而且在這個(gè)接口里沒(méi)有提供寫入數(shù)據(jù)和讀取數(shù)據(jù)的能力2023-01-01Android 防止多次重復(fù)點(diǎn)擊的三種方法的示例
本篇文章主要介紹了Android 防止多次重復(fù)點(diǎn)擊的三種方法的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03Android 掃描附近的藍(lán)牙設(shè)備并連接藍(lán)牙音響的示例
本篇文章主要介紹了Android 掃描附近的藍(lán)牙設(shè)備并連接藍(lán)牙音響的示例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09輕松實(shí)現(xiàn)Android仿淘寶地區(qū)選擇功能
這篇文章主要介紹了輕松實(shí)現(xiàn)Android仿淘寶地區(qū)選擇功能的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06Android OkHttp實(shí)現(xiàn)全局過(guò)期token自動(dòng)刷新示例
本篇文章主要介紹了Android OkHttp實(shí)現(xiàn)全局過(guò)期token自動(dòng)刷新示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03Android開發(fā)實(shí)現(xiàn)讀取assets目錄下db文件的方法示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)讀取assets目錄下db文件的方法,結(jié)合實(shí)例形式分析了Android針對(duì)assets目錄下SQLite數(shù)據(jù)庫(kù)文件的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10Android 實(shí)現(xiàn)左滑出現(xiàn)刪除選項(xiàng)
滑動(dòng)刪除的部分主要包含兩個(gè)部分, 一個(gè)是內(nèi)容區(qū)域(用于放置正常顯示的view),另一個(gè)是操作區(qū)域(用于放置刪除按鈕)。下面通過(guò)本文給大家介紹Android 實(shí)現(xiàn)左滑出現(xiàn)刪除選項(xiàng),需要的朋友可以參考下2017-06-06