Android — 圖片的特效處理

來源:互聯網
上載者:User

Android --- 圖片處理的方法

轉換 -  drawable To  bitmap

縮放 -  Zoom

圓角 -  Round Corner

倒影 -  Reflected


bitmapPrcess  code:

package com.learn.games;</p><p>import android.graphics.Bitmap;<br />import android.graphics.Canvas;<br />import android.graphics.LinearGradient;<br />import android.graphics.Matrix;<br />import android.graphics.Paint;<br />import android.graphics.PixelFormat;<br />import android.graphics.PorterDuffXfermode;<br />import android.graphics.Rect;<br />import android.graphics.RectF;<br />import android.graphics.Bitmap.Config;<br />import android.graphics.PorterDuff.Mode;<br />import android.graphics.Shader.TileMode;<br />import android.graphics.drawable.Drawable;</p><p>public class bitmapProcess {</p><p>// zoom<br />public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h){<br />int width = bitmap.getWidth();<br />int height = bitmap.getHeight();<br />Matrix matrix = new Matrix();</p><p>float scaleWidth = w/(float)width;<br />float scaleHeight = h/(float)height;<br />matrix.postScale(scaleWidth, scaleHeight);</p><p>Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);<br />return bitmap2;<br />}</p><p>// drawable to bitmap<br />public static Bitmap drawable2Bitmap(Drawable drawable){<br />int width = drawable.getIntrinsicHeight();<br />int height = drawable.getIntrinsicHeight();</p><p>Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity()<br />!= PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);</p><p>Canvas canvas = new Canvas(bitmap);<br />drawable.setBounds(0, 0, width, height);<br />drawable.draw(canvas);</p><p>return bitmap;<br />}</p><p>// Round Corner Bitmap<br />public static Bitmap getRoundCornerBitmap(Bitmap bitmap, float roundPX){<br />int width = bitmap.getWidth();<br />int height = bitmap.getHeight();</p><p>Bitmap bitmap2 = Bitmap.createBitmap(width, height, Config.ARGB_8888);<br />Canvas canvas = new Canvas(bitmap2);</p><p>final int color = 0xff424242;<br />final Paint paint = new Paint();<br />final Rect rect = new Rect(0, 0, width, height);<br />final RectF rectF = new RectF(rect);</p><p>paint.setColor(color);<br />paint.setAntiAlias(true);<br />canvas.drawARGB(0, 0, 0, 0);<br />canvas.drawRoundRect(rectF, roundPX, roundPX, paint);</p><p>paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));<br />canvas.drawBitmap(bitmap, rect, rect, paint);</p><p>return bitmap2;<br />}</p><p>// Reflect Bitmap<br />public static Bitmap createReflectedBitmap(Bitmap bitmap){<br />final int reflectedGap = 4;<br />int width = bitmap.getWidth();<br />int height = bitmap.getHeight();</p><p>Matrix matrix = new Matrix();<br />matrix.preScale(1, -1);</p><p>Bitmap reflectedImage = Bitmap.createBitmap(bitmap, 0, height/2, width, height/2, matrix, false);<br />Bitmap reflectedBitmap = Bitmap.createBitmap(width, (height + height/2), Config.ARGB_8888);</p><p>Canvas canvas = new Canvas(reflectedBitmap);<br />canvas.drawBitmap(bitmap, 0, 0, null);<br />Paint defaultPaint = new Paint();<br />canvas.drawRect(0, height, width, height + reflectedGap, defaultPaint);<br />canvas.drawBitmap(reflectedImage, 0, height + reflectedGap, null);</p><p>Paint paint = new Paint();<br />LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,<br />reflectedBitmap.getHeight() + reflectedGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);<br />paint.setShader(shader);<br />paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));<br />canvas.drawRect(0, height, width, reflectedBitmap.getHeight() + reflectedGap, paint);</p><p>return reflectedBitmap;<br />}</p><p>}<br />

Java Code:

package com.learn.games;</p><p>import android.app.Activity;<br />import android.graphics.Bitmap;<br />import android.graphics.drawable.BitmapDrawable;<br />import android.graphics.drawable.Drawable;<br />import android.os.Bundle;<br />import android.widget.ImageView;</p><p>public class MyBitmapProcessActivity extends Activity {<br />private ImageView imgView1;<br />private ImageView imgView2;<br />private ImageView imgView3;<br />private ImageView imgView4;</p><p> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);</p><p> drawBitmap();<br /> }</p><p> private void drawBitmap(){<br /> imgView1 = (ImageView)findViewById(R.id.imgView1);<br /> imgView2 = (ImageView)findViewById(R.id.imgView2);<br /> imgView3 = (ImageView)findViewById(R.id.imgView3);<br /> imgView4 = (ImageView)findViewById(R.id.imgView4);</p><p> Drawable drawable = this.getWallpaper();</p><p> Bitmap bitmap = bitmapProcess.drawable2Bitmap(drawable);// drawable to bitmap<br /> Bitmap zoomBitmap = bitmapProcess.zoomBitmap(bitmap, 100, 100);// zoom<br /> Bitmap roundBitmap = bitmapProcess.getRoundCornerBitmap(zoomBitmap, 10.0f);// round corner<br /> Bitmap reflectedBitmap = bitmapProcess.createReflectedBitmap(zoomBitmap);// reflect bitmap</p><p> // drawable to bitmap<br /> imgView1.setImageBitmap(bitmap);<br /> imgView2.setImageBitmap(zoomBitmap);<br /> imgView3.setImageBitmap(roundBitmap);<br /> imgView4.setImageBitmap(reflectedBitmap);</p><p> // bitmap to drawable<br /> Drawable roundDrawable = new BitmapDrawable(roundBitmap);<br /> Drawable reflectedDrawable = new BitmapDrawable(reflectedBitmap);<br /> imgView1.setBackgroundDrawable(roundDrawable);<br /> imgView2.setBackgroundDrawable(reflectedDrawable);<br /> }<br />}

XML:

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ></p><p> <ImageView android:id="@+id/imgView1"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"/></p><p> <ImageView android:id="@+id/imgView2"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"/></p><p> <ImageView android:id="@+id/imgView3"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"/></p><p> <ImageView android:id="@+id/imgView4"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"/></p><p></LinearLayout><br />

drawable2bitmap

zoom

round corner

reflected bitmap

相關文章

聯繫我們

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