Bitamp產生倒影

來源:互聯網
上載者:User

main.activity如下:

package c.c;import android.os.Bundle;import android.widget.ImageView;import android.app.Activity;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;/** * Demo:產生Bitmap的倒影 */public class MainActivity extends Activity {private ImageView mImageView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mImageView=(ImageView) findViewById(R.id.imageView);Bitmap rawBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.a);Bitmap newBitmap=getBitmapReflection(rawBitmap);mImageView.setImageBitmap(newBitmap);}private Bitmap getBitmapReflection(Bitmap rawBitmap) {final int gap = 4;int width = rawBitmap.getWidth();int height = rawBitmap.getHeight();Matrix matrix = new Matrix();//逆置matrix.preScale(1, -1);//倒影Bitmap reflectionBitmap =Bitmap.createBitmap(rawBitmap, 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(rawBitmap, 0, 0, null);Paint paint=new Paint();//畫分界線canvas.drawRect(0, height, width, height + gap, paint);//畫倒影canvas.drawBitmap(reflectionBitmap, 0, height + gap, null);//沿Y軸漸層效果LinearGradient shader = new LinearGradient(0, rawBitmap.getHeight(), 0,bitmapWithReflection.getHeight() + gap, 0x70ffffff,0x00ffffff, TileMode.CLAMP);paint.setShader(shader);//因為有兩層(倒影層和漸層),所以設定過渡模式paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));//畫一個具有漸層效果的矩形canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()+ gap, paint);return bitmapWithReflection;}}

main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <ImageView        android:id="@+id/imageView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"      /></RelativeLayout>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.