




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
【移動應(yīng)用開發(fā)技術(shù)】Android開發(fā)如何使用自定義View將圓角矩形繪制在Canvas上的方法
這篇文章主要介紹了Android開發(fā)如何使用自定義View將圓角矩形繪制在Canvas上的方法,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓在下帶著大家一起了解一下。Android開發(fā)使用自定義View將圓角矩形繪制在Canvas上的方法,具體如下:前幾天,公司一個(gè)項(xiàng)目中,頭像圖片需要添加圓角,這樣UI效果會更好看,于是寫了一個(gè)小的demo進(jìn)行圓角的定義,該處主要是使用BitmapShader進(jìn)行了渲染(如果要將一張圖片裁剪成橢圓或圓形顯示在屏幕上,也可以使用BitmapShader來完成).BitmapShader類完成渲染圖片的基本步驟如下:1、創(chuàng)建BitmapShader類的對象
/**
*
Call
this
to
create
a
new
shader
that
will
draw
with
a
bitmap.
*
*
@param
bitmap
The
bitmap
to
use
inside
the
shader
*
@param
tileX
The
tiling
mode
for
x
to
draw
the
bitmap
in.
*
@param
tileY
The
tiling
mode
for
y
to
draw
the
bitmap
in.
*/
public
BitmapShader(Bitmap
bitmap,
TileMode
tileX,
TileMode
tileY)
{
}其中,Shader.TitleMode類型有三種,CALMP、MIRROR、REPEATCALMP:使用邊界顏色來填充剩余空間MIRROR:使用鏡像方式REPEAT:使用重復(fù)方式2、通過Paint的setShader(bitmapShafer)來設(shè)置畫筆3、使用已經(jīng)setShader(bitmapShafer)的畫筆來繪制圖形下面展示繪制圓角圖片的demo1、自定義RounderCornerImageView.java類package
com.example.test;
import
android.content.Context;
import
android.graphics.Bitmap;
import
android.graphics.BitmapShader;
import
android.graphics.Canvas;
import
android.graphics.Matrix;
import
android.graphics.Paint;
import
android.graphics.RectF;
import
android.graphics.Shader;
import
android.util.AttributeSet;
import
android.view.View;
public
class
RounderCornerImageView
extends
View
{
private
Bitmap
mImage;//
source
bitmap
private
Paint
mBitmapPaint;//paint
private
RectF
mBrounds;//rect
private
float
mRadius=20.0f;//round
public
RounderCornerImageView(Context
context)
{
this(context,
null);
}
public
RounderCornerImageView(Context
context,
AttributeSet
attrs)
{
this(context,
attrs,
0);
}
public
RounderCornerImageView(Context
context,
AttributeSet
attrs,
int
defStyleAttr)
{
super(context,
attrs,
defStyleAttr);
init();
}
private
void
init()
{
mBitmapPaint=new
Paint(Paint.ANTI_ALIAS_FLAG);
mBrounds=new
RectF();
}
@Override
protected
void
onMeasure(int
widthMeasureSpec,
int
heightMeasureSpec)
{
//
TODO
Auto-generated
method
stub
int
height,width;
height=width=0;
//obtain
bitmap
size
int
imageHeight,imageWidth;
if
(null!=mImage)
{
imageHeight=imageWidth=0;
}else
{
imageHeight=mImage.getHeight();
imageWidth=mImage.getWidth();
}
//obtain
best
measure
data
and
set
on
View
width=getMeasurement(widthMeasureSpec,imageWidth);
height=getMeasurement(heightMeasureSpec,
imageHeight);
//set
View
last
size
setMeasuredDimension(width,
height);
}
/**
*
measure
width
and
height
by
specMode
**/
private
int
getMeasurement(int
measureSpec,
int
contentSize)
{
int
specSize=MeasureSpec.getSize(measureSpec);
switch
(MeasureSpec.getMode(measureSpec))
{
case
MeasureSpec.AT_MOST:
return
Math.min(specSize,
contentSize);
case
MeasureSpec.UNSPECIFIED:
return
contentSize;
case
MeasureSpec.EXACTLY:
return
specSize;
default:
return
0;
}//switch
}
@Override
protected
void
onSizeChanged(int
w,
int
h,
int
oldw,
int
oldh)
{
if
(w!=oldw
||
h!=oldh)
{
int
imageWidth,imageHeight;
if
(null==mImage)
{
imageWidth=imageHeight=0;
}else
{
imageWidth=mImage.getWidth();
imageHeight=mImage.getHeight();
}
//center
point
int
left=(w-imageWidth)/2;
int
top=(h-imageHeight)/2;
mBrounds.set(left,
top,
left+imageWidth,
top+imageHeight);
if
(null!=mBitmapPaint.getShader())
{
Matrix
m=new
Matrix();
m.setTranslate(left,
top);
mBitmapPaint.getShader().setLocalMatrix(m);
}
}
}
public
void
setImage(Bitmap
bitmap)
{
if
(mImage!=bitmap)
{
mImage=bitmap;
if
(null!=mImage)
{
BitmapShader
shader=new
BitmapShader(bitmap,
Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP);
mBitmapPaint.setShader(shader);
}else
{
mBitmapPaint.setShader(null);
}
requestLayout();//invalidated
the
layout
of
this
view
by
onDraw()
}
}
@Override
protected
void
onDraw(Canvas
canvas)
{
super.onDraw(canvas);
if
(null!=mBitmapPaint)
{
//draw
Round
Rect
canvas.drawRoundRect(mBrounds,
mRadius,
mRadius,
mBitmapPaint);
}
}
}2、顯示圓角圖片的RoundActivity.java類package
com.example.test;
import
android.app.Activity;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.os.Bundle;
public
class
RoundActivity
extends
Activity{
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
RounderCornerImageView
view=new
RounderCornerImageView(this);
Bitmap
souBitmap=BitmapFactory.decodeResource(getResources(),
R.drawable.sun);
view.setImage(souBitmap);
setContentView(view);
}
}另外,附注下自定義View的一些基本步驟和必須實(shí)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度食品飲料銷售顧問勞動合同補(bǔ)充協(xié)議
- 二零二五年度快遞配送與客戶關(guān)系管理合同
- 二零二五年度個(gè)人經(jīng)營貸款委托銀行合同
- 二零二五年度魚塘承包與漁業(yè)品牌建設(shè)合作協(xié)議
- 二零二五年度工程內(nèi)架架子工工人高空作業(yè)安全保障合同
- 年婚婚前協(xié)議之2025年度婚姻風(fēng)險(xiǎn)防控全面保障合同
- 幫助抵押貸款合同的指南
- 工作時(shí)間管理協(xié)議
- 企業(yè)合并中介服務(wù)協(xié)議
- 資源安全對國家安全的影響課件高中地理人教版選擇性必修三
- 2023屆高三英語二輪復(fù)習(xí)讀后續(xù)寫情感描寫素材(比賽收獲脫困脫險(xiǎn))
- 染色體實(shí)驗(yàn)室質(zhì)量控制
- NB/T 10742-2021智能化綜采工作面設(shè)計(jì)規(guī)范
- YY/T 0967-2022牙科學(xué)旋轉(zhuǎn)和往復(fù)運(yùn)動器械的桿
- GB/T 2934-2007聯(lián)運(yùn)通用平托盤主要尺寸及公差
- GB/T 29334-2012用于非石油基液壓制動液的汽車液壓制動缸用的彈性體皮碗和密封圈
- GB/T 14706-1993校對符號及其用法
- 高二數(shù)學(xué)平面向量的基本定理
- 高中美術(shù)-美術(shù)鑒賞《審美自律》
- 強(qiáng)讀和弱讀-(課堂)課件
- 裕興新概念英語第二冊筆記第42課
評論
0/150
提交評論