Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android實現圖片圓角代碼

android實現圖片圓角代碼

編輯:關於Android編程

圓角效果:



import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;

public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
Drawable drawable = getResources().getDrawable(R.drawable.jpg1);
BitmapDrawable bitmapDrawable = (BitmapDrawable)drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();

ImageView imageView = (ImageView)findViewById(R.id.imgShow);
imageView.setImageBitmap(Main.getRoundedCornerBitmap(bitmap));

}
// 將圖片的四角圓化
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {

Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
//得到畫布
Canvas canvas = new Canvas(output);


//將畫布的四角圓化
final int color = Color.RED;
final Paint paint = new Paint();
//得到與圖像相同大小的區域 由構造的四個值決定區域的位置以及大小
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
//值越大角度越明顯
final float roundPx = 50;

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
//drawRoundRect的第2,3個參數一樣則畫的是正圓的一角,如果數值不同則是橢圓的一角
canvas.drawRoundRect(rectF, roundPx,roundPx, paint);

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);

return output;
}

}

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