Android實(shí)現(xiàn)帶描邊的圓角圖片
利用學(xué)過(guò)的BitmapShader渲染類(lèi),我們來(lái)實(shí)現(xiàn)一個(gè)帶描邊的圓角圖片。
具體實(shí)現(xiàn):
用來(lái)顯示自定義的繪圖類(lèi)的布局文件
res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/frameLayout1" android:orientation="vertical" > </FrameLayout>
打開(kāi)MainActivity,在文件中創(chuàng)建名為MyView的內(nèi)部類(lèi),繼承android.view.View類(lèi),并添加構(gòu)造方法和重寫(xiě)onDraw(Canvas canvas)方法,在里面進(jìn)行作圖:
在onDraw(Canvas canvas)方法中,首先定義一個(gè)畫(huà)筆,并設(shè)置其使用抗鋸齒功能,然后定義一張背景,然后定義一個(gè)要繪制的圓角矩形的區(qū)域,并將畫(huà)布在X軸上平移40像素,在Y軸上平移20像素,再繪制一個(gè)黑色的2像素的圓角矩形,作為圖片的邊,最后繪制一個(gè)使用BitmapShader渲染的圓角矩形圖片,具體代碼如下:
MainActivity:
package com.example.test;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.graphics.Shader.TileMode;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//獲取布局文件中添加的幀布局管理器
FrameLayout fl=(FrameLayout)findViewById(R.id.frameLayout1);
//將自定義的MyView視圖添加到幀布局
fl.addView(new MyView(this));
}
public class MyView extends View{
private float view_width=300;
private float view_height=300;
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint=new Paint();
paint.setAntiAlias(true);
Bitmap bitmap_bg=BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.backgroud);
canvas.drawBitmap(bitmap_bg, 0, 0,paint);//繪制背景
RectF rect=new RectF(0,0,280,180);
canvas.translate(40, 20);//將畫(huà)布在X軸上平移40像素,在Y軸上平移20像素
//為圖片添加描邊
paint.setStyle(Style.STROKE);//設(shè)置填充樣式為描邊
paint.setColor(Color.BLACK);//設(shè)置顏色為黑色
paint.setStrokeWidth(2);//設(shè)置筆觸寬度為2像素
canvas.drawRoundRect(rect, 10, 10, paint);//繪制一個(gè)描邊的圓角矩形
paint.setStyle(Style.FILL);//設(shè)置填充樣式為填充
Bitmap bm=BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.img1);
//創(chuàng)建一個(gè)在水平方向重復(fù),在豎直方向鏡像的BitmapShader對(duì)象
BitmapShader bs=new BitmapShader(bm,TileMode.REPEAT,TileMode.MIRROR);
paint.setShader(bs);//設(shè)置渲染對(duì)象
//繪制一個(gè)使用BitmapShader渲染的圓角矩形圖片
canvas.drawRoundRect(rect, 10, 10, paint);
}
}
}
運(yùn)行效果如圖所示

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android自定義imageview實(shí)現(xiàn)圓角圖片
- Android實(shí)現(xiàn)圓形圖片或者圓角圖片
- Android 實(shí)現(xiàn)圓角圖片的簡(jiǎn)單實(shí)例
- Android中實(shí)現(xiàn)圓角圖片的幾種方法
- Android中Glide加載圓形圖片和圓角圖片實(shí)例代碼
- Android如何設(shè)置圓角圖片
- Android關(guān)于Glide的使用(高斯模糊、加載監(jiān)聽(tīng)、圓角圖片)
- android 實(shí)現(xiàn)圓角圖片解決方案
- android 設(shè)置圓角圖片實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)圓角圖片的方法
相關(guān)文章
Android手機(jī)拍照或選取圖庫(kù)圖片作為頭像
這篇文章主要介紹了Android手機(jī)拍照或選取圖庫(kù)圖片作為頭像的相關(guān)資料,需要的朋友可以參考下2015-06-06
Android實(shí)現(xiàn)倒計(jì)時(shí)結(jié)束后跳轉(zhuǎn)頁(yè)面功能
最近在工作中遇到一個(gè)需求,需要在倒計(jì)時(shí)一段時(shí)間后進(jìn)行跳轉(zhuǎn)頁(yè)面,通過(guò)查找相關(guān)資料發(fā)現(xiàn)其中涉及的知識(shí)還不少,所以分享出來(lái),下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)倒計(jì)時(shí)結(jié)束后跳轉(zhuǎn)頁(yè)面功能的相關(guān)資料,需要的朋友可以參考下。2017-11-11
深入學(xué)習(xí)Android?ANR?的原理分析及解決辦法
Android系統(tǒng)中,AMS和WMS會(huì)檢測(cè)App的響應(yīng)時(shí)間,如果App在特定時(shí)間無(wú)法相應(yīng)屏幕觸摸或鍵盤(pán)輸入時(shí)間,或者特定事件沒(méi)有處理完畢,就會(huì)出現(xiàn)ANR。本文將帶領(lǐng)大學(xué)深入學(xué)習(xí)一下ANR的原理及解決辦法,感興趣的同學(xué)可以學(xué)習(xí)一下2021-11-11
android項(xiàng)目從Eclipse遷移到Android studio中常見(jiàn)問(wèn)題解決方法
android項(xiàng)目從Eclipse遷移到Android studio中經(jīng)常會(huì)遇到一些問(wèn)題,本文提供了Android studio使用中常見(jiàn)問(wèn)題解決方法2018-03-03
Android音視頻開(kāi)發(fā)Media FrameWork框架源碼解析
這篇文章主要為大家介紹了Android音視頻開(kāi)發(fā)Media FrameWork框架源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
android實(shí)現(xiàn)常駐通知欄遇到的問(wèn)題及解決辦法
這篇文章主要介紹了android實(shí)現(xiàn)常駐通知欄遇到的問(wèn)題及解決辦法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Android?RecyclerView使用ListAdapter高效刷新數(shù)據(jù)的操作方法
這篇文章主要介紹了Android?RecyclerView使用ListAdapter高效刷新數(shù)據(jù),本次也是介紹了用另外一種方法來(lái)實(shí)現(xiàn)RecyclerView高效刷新數(shù)據(jù)的功能,需要的朋友可以參考下2022-10-10
Android自定義帶有圓形進(jìn)度條的可長(zhǎng)按控件功能
這篇文章主要介紹了Android自定義帶有圓形進(jìn)度條的可長(zhǎng)按控件,思路很簡(jiǎn)單,使用簡(jiǎn)單的畫(huà)筆工具就可以完成這個(gè)控件,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-06-06
Android編程中File文件常見(jiàn)存儲(chǔ)與讀取操作demo示例
這篇文章主要介紹了Android編程中File文件常見(jiàn)存儲(chǔ)與讀取操作,結(jié)合實(shí)例形式分析了Android針對(duì)文件的打開(kāi)、讀寫(xiě)及布局等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09

