倒影效果的實現

來源:互聯網
上載者:User
package com.example.demoshadow;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Matrix;import android.view.SurfaceHolder;import android.view.SurfaceView;/** *  * @author turlet *  */public class ImageShadowView extends SurfaceView implementsSurfaceHolder.Callback {private Bitmap mBitmap;public ImageShadowView(Context context) {super(context);SurfaceHolder holder = this.getHolder();holder.addCallback(this);mBitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.spinner_background);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);int width = mBitmap.getWidth();int height = mBitmap.getHeight();canvas.drawBitmap(mBitmap, 0, 0, null);Bitmap nBitmap = setShadow(mBitmap);nBitmap = setAlpha(nBitmap, 50);// 一半透明canvas.drawBitmap(nBitmap, 0, height + 2, null);}public void surfaceCreated(SurfaceHolder holder) {Canvas canvas = holder.lockCanvas();this.onDraw(canvas);holder.unlockCanvasAndPost(canvas);}public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {}public void surfaceDestroyed(SurfaceHolder holder) {}/** * @param sourceImg *            傳入的圖片 * @param number *            0-100(0為完全透明,100為不透明) * @return Bitmap 處理後的圖片 */public static Bitmap setAlpha(Bitmap sourceImg, int number) {int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0,sourceImg.getWidth(), sourceImg.getHeight());number = number * 255 / 100;double round = (double) number / (double) (argb.length);System.out.println(round + "  l=" + argb.length + " n=" + number);for (int i = 0; i < argb.length; i++) {if (number - i * round > 10) {argb[i] = ((int) (number - i * round) << 24)| (argb[i] & 0x00FFFFFF);continue;} else {argb[i] = (10 << 24) | (argb[i] & 0x00FFFFFF);continue;}}sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(),sourceImg.getHeight(), Config.ARGB_8888);return sourceImg;}/** * 傳入一張圖片,經倒轉後,再取一半 * @param bitmap * @return */public static Bitmap setShadow(Bitmap bitmap) {int width = bitmap.getWidth();int height = bitmap.getHeight();Matrix matrix = new Matrix();matrix.preScale(1, -1);Bitmap shadowImage = Bitmap.createBitmap(bitmap, 0, height / 2, width,height / 2, matrix, false);return shadowImage;}}

聯繫我們

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