android 設(shè)置圓角圖片實(shí)現(xiàn)代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</LinearLayout>
package com.test.demo;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MyActivity 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.bg);
// BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
// Bitmap bitmap = bitmapDrawable.getBitmap();
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
Drawable drawable = getResources().getDrawable(R.drawable.bg);
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();
BitmapDrawable bbb = new BitmapDrawable(toRoundCorner(bitmap, 30));
layout.setBackgroundDrawable(bbb);
//ImageView imageView = (ImageView) findViewById(R.id.imgShow);
//imageView.setImageBitmap(MyActivity.getRoundedCornerBitmap(bitmap));
//imageView.setImageBitmap(MyActivity.toRoundCorner(bitmap, 20));
}
public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
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 = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
}
相關(guān)文章
Android開發(fā)中Activity創(chuàng)建跳轉(zhuǎn)及傳值的方法
這篇文章主要介紹了Android開發(fā)中Activity創(chuàng)建跳轉(zhuǎn)及傳值的方法的相關(guān)資料,需要的朋友可以參考下2016-05-05Android 登錄頁(yè)面的實(shí)現(xiàn)代碼(密碼顯示隱藏、EditText 圖標(biāo)切換、限制輸入長(zhǎng)度)
這篇文章主要介紹了Android 登錄頁(yè)面的實(shí)現(xiàn)代碼(密碼顯示隱藏、EditText 圖標(biāo)切換、限制輸入長(zhǎng)度),本文通過兩種方法給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08android實(shí)現(xiàn)定時(shí)拍照功能
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)定時(shí)拍照功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android學(xué)習(xí)筆記-保存數(shù)據(jù)到SQL數(shù)據(jù)庫(kù)中(Saving Data in SQL Databases)
這篇文章主要介紹了Android學(xué)習(xí)筆記-保存數(shù)據(jù)到SQL數(shù)據(jù)庫(kù)中的(Saving Data in SQL Databases)2014-10-10Android實(shí)現(xiàn)抽獎(jiǎng)轉(zhuǎn)盤實(shí)例代碼
這篇文章主要介紹了Android實(shí)現(xiàn)抽獎(jiǎng)轉(zhuǎn)盤實(shí)例代碼,可以應(yīng)用于Android游戲開發(fā)中的一個(gè)應(yīng)用,需要的朋友可以參考下2014-07-07Android開發(fā)TextvView實(shí)現(xiàn)鏤空字體效果示例代碼
這篇文章主要介紹了Android開發(fā)TextvView實(shí)現(xiàn)鏤空字體效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10