如何實現倒影效果
package com.easymorse.picture;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imageView2 = (ImageView) findViewById(R.id.picture_qian);
Bitmap bmp = ((BitmapDrawable) getResources().getDrawable(
R.drawable.qian)).getBitmap();
imageView2.setImageBitmap(createReflectedImage(bmp));
}
public static Bitmap createReflectedImage(Bitmap originalImage) {
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 defaultPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
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);
return bitmapWithReflection;
}
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imageView2 = (ImageView) findViewById(R.id.picture_qian);
Bitmap bmp = ((BitmapDrawable) getResources().getDrawable(
R.drawable.qian)).getBitmap();
imageView2.setImageBitmap(createReflectedImage(bmp));
}
public static Bitmap createReflectedImage(Bitmap originalImage) {
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 defaultPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
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);
return bitmapWithReflection;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageView android:id="@+id/picture_qian"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:src="@drawable/qian" />
</LinearLayout>
相關文章
- 如何使用Java程式碼給圖片增加倒影效果Java
- CSS3帶有倒影的文字效果CSSS3
- CSS 如何實現羽化效果?CSS
- 巧用 -webkit-box-reflect 倒影實現各類動效WebKit
- Flutter吸附效果如何實現?Flutter
- css如何實現div全屏效果CSS
- 如何實現全屏遮罩層效果遮罩
- CSS如何實現柱狀效果CSS
- Web 頁面如何實現動畫效果Web動畫
- 如何實現div可編輯效果
- CSS如何實現div的透明效果CSS
- CSS如何實現底線重合效果CSS
- 報表如何實現行列互換效果?
- 教你如何使用MvcPager實現分頁效果MVC
- 如何用canvas實現大波紋灌水效果Canvas
- 如何實現span元素垂直水平居中效果
- javascript如何實現二維陣列效果JavaScript陣列
- 如何實現兩個div等高效果
- jQuery如何實現tab選項卡效果jQuery
- 如何實現div水平和垂直居中效果
- 剪映倒影幻燈片特效如何開啟?剪映倒影幻燈片特效的設定方法特效
- 報表展現時如何實現固定表頭效果
- 分析Guitar Pro 7如何實現切音效果GUI
- 如何在 ThreeJS 中實現輝光效果JS
- Swift如何純程式碼實現時鐘效果Swift
- 如何實現點選連結不跳轉效果
- DW如何實現程式碼行縮排效果
- 如何從零實現一個詞雲效果
- 實現聚焦效果
- 教你如何實現 Android TextView 文字輪播效果AndroidTextView
- axure教程:如何實現數字輸入框效果
- 「譯」如何實現互動式 WebGL 懸停效果Web
- PPT中如何實現川流不息的動畫效果動畫
- css如何實現div中的文字垂直居中效果CSS
- 如何使用CCMenuItemImage實現CCMenuItemToggle的開關效果UI
- 如何快速實現一個無縫輪播效果
- webgl實現故障效果Web
- webgl實現火焰效果Web