Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 中實現圖片倒影效果

android 中實現圖片倒影效果

編輯:關於Android編程

1、效果圖:

\

\

2、核心代碼:

<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;">package com.example.pic_reflection; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; 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.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView img = null; private ImageView orginalImg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.orginalImg = (ImageView) this.findViewById(R.id.img); this.orginalImg.setBackgroundResource(R.drawable.b); this.img = (ImageView) this.findViewById(R.id.reflection1); this.img.setImageBitmap(createReflectedImage(((BitmapDrawable) this.getResources().getDrawable(R.drawable.b)).getBitmap(), 110)); } public static Bitmap createReflectedImage(Bitmap originalImage, int reflectionHeight) { int width = originalImage.getWidth(); int height = originalImage.getHeight(); Matrix matrix = new Matrix(); // 實現圖片翻轉90度 matrix.preScale(1, -1); if (reflectionHeight > height) reflectionHeight = height; // 創建倒影圖片(是原始圖片的一半大小) Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height - reflectionHeight, width, reflectionHeight, matrix, false); // 創建倒影圖片 Bitmap finalReflection = Bitmap.createBitmap(width, reflectionHeight, Config.ARGB_8888); // 創建畫布 Canvas canvas = new Canvas(finalReflection); // canvas.drawBitmap(originalImage, 0, 0, null); // 把倒影圖片畫到畫布上 canvas.drawBitmap(reflectionImage, 0, 0, null); Paint shaderPaint = new Paint(); // 創建線性漸變LinearGradient對象 LinearGradient shader = new LinearGradient(0, 0, 0, finalReflection.getHeight() + 1, 0x70ffffff, 0x00ffffff, TileMode.MIRROR); shaderPaint.setShader(shader); shaderPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // 畫布畫出反轉圖片大小區域,然後把漸變效果加到其中,就出現了圖片的倒影效果。 canvas.drawRect(0, 0, width, finalReflection.getHeight(), shaderPaint); return finalReflection; } } 3、main.xml




    

    


4、activity_main.xml


<frameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    

    

</frameLayout>



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved