android 中實現圖片倒影效果

來源:互聯網
上載者:User

標籤:android   blog   http   java   color   os   

1、:



2、核心代碼:

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 

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="vertical" >    <include layout="@layout/activity_main" />    <ImageView        android:id="@+id/reflection1"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

4、activity_main.xml

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content" >    <ImageView        android:id="@+id/img"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scaleType="fitXY" />    <TextView        android:id="@+id/lblBanner"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_gravity="bottom"        android:background="#88000000"        android:ellipsize="end"        android:gravity="center_horizontal|center_vertical"        android:singleLine="true"        android:text="小汽車一枚"        android:textColor="#FFF" /></FrameLayout>



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.