亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android 實現(xiàn)圓角圖片解決方案

 更新時間:2012年11月30日 15:38:42   作者:  
現(xiàn)在我們就來看看怎么樣把圖片的四角都變成圓形的,為什么要這樣做那,可能是為了美化界面吧,下面我們就來看看代碼吧
現(xiàn)在我們就來看看怎么樣把圖片的四角都變成圓形的,為什么要這樣做那,如果要是這樣界面就會非常的美觀,下面我們就來看看代碼吧。
java代碼:
復制代碼 代碼如下:

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;
}

當我們需要圓角的時候,調(diào)用這個方法,第一個參數(shù)是傳入需要轉(zhuǎn)化成圓角的圖片,第二個參數(shù)是圓角的度數(shù),數(shù)值越大,圓角越大
下面興趣個例子,我有一個LinearLayout,我想把它的背景圖片設置成圓角
布局文件main.xml
java代碼:
復制代碼 代碼如下:

<?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>

java代碼:
復制代碼 代碼如下:

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;
}
}

相關文章

  • Android自定義TimeButton實現(xiàn)倒計時按鈕

    Android自定義TimeButton實現(xiàn)倒計時按鈕

    這篇文章主要為大家詳細介紹了Android自定義TimeButton實現(xiàn)倒計時按鈕,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • Android巧用XListView實現(xiàn)萬能下拉刷新控件

    Android巧用XListView實現(xiàn)萬能下拉刷新控件

    這篇文章主要為大家詳細介紹了Android巧用XListView實現(xiàn)萬能下拉刷新控件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Flutter實現(xiàn)掃二維碼功能

    Flutter實現(xiàn)掃二維碼功能

    這篇文章主要為大家詳細介紹了Flutter實現(xiàn)掃二維碼功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Android View進行手勢識別詳解

    Android View進行手勢識別詳解

    本文主要介紹 Android View進行手勢識別,這里整理了相關資料和簡單示例,有興趣的小伙伴可以參考下
    2016-08-08
  • OpenGL ES透視投影實現(xiàn)方法(四)

    OpenGL ES透視投影實現(xiàn)方法(四)

    這篇文章主要為大家詳細介紹了OpenGL ES透視投影的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android自定義View圓形和拖動圓跟隨手指拖動

    Android自定義View圓形和拖動圓跟隨手指拖動

    這篇文章主要介紹了Android自定義View圓形和拖動圓跟隨手指拖動,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android項目開發(fā) 教你實現(xiàn)Periscope點贊效果

    Android項目開發(fā) 教你實現(xiàn)Periscope點贊效果

    這篇文章主要為大家分享了Android項目開發(fā),一步一步教你實現(xiàn)Periscope點贊效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2015-12-12
  • Android Toolbar應用欄使用方法簡介

    Android Toolbar應用欄使用方法簡介

    App中應用欄是十分常見的,通常應用欄會顯示當前頁面的標題,還有一些操作按鈕,例如返回、搜索、掃碼等。本文介紹如何通過Toolbar實現(xiàn)應用欄
    2022-12-12
  • android不讀入內(nèi)存獲取圖像寬高信息的方法

    android不讀入內(nèi)存獲取圖像寬高信息的方法

    這篇文章主要介紹了android不讀入內(nèi)存獲取圖像寬高信息的方法,涉及Android獲取圖像信息的相關技巧,需要的朋友可以參考下
    2015-04-04
  • Android?搜索框架使用詳解

    Android?搜索框架使用詳解

    這篇文章主要為大家介紹了Android?搜索框架使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11

最新評論