編輯:關於Android編程
本文實例講述了android實現圓角矩形背景的方法。分享給大家供大家參考。具體如下:
1. java代碼如下:
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.view.MotionEvent;
public class RoundRectDradable extends Drawable{
private static final float DEFAULT_RADIUS = 6.f;
private Paint mPaint = new Paint();
private RoundRectShape mShape;
private float[] mOuter;
private int mColor;
private int mPressColor;
private float mTopLeftRadius = DEFAULT_RADIUS;
private float mTopRightRadius = DEFAULT_RADIUS;
private float mBottomLeftRadius = DEFAULT_RADIUS;
private float mBottomRightRadius = DEFAULT_RADIUS;
public RoundRectDradable() {
mColor = Color.WHITE;
mPressColor = Color.WHITE;
mPaint.setColor(mColor);
mPaint.setAntiAlias(true);
}
public float getTopLeftRadius() {
return mTopLeftRadius;
}
public void setTopLeftRadius(float topLeftRadius) {
this.mTopLeftRadius = topLeftRadius;
}
public float getTopRightRadius() {
return mTopRightRadius;
}
public void setTopRightRadius(float topRightRadius) {
this.mTopRightRadius = topRightRadius;
}
public float getBottomLeftRadius() {
return mBottomLeftRadius;
}
public void setBottomLeftRadius(float bottomLeftRadius) {
this.mBottomLeftRadius = bottomLeftRadius;
}
public float getBottomRightRadius() {
return mBottomRightRadius;
}
public void setBottomRightRadius(float bottomRightRadius) {
this.mBottomRightRadius = bottomRightRadius;
}
public int getPressColor() {
return mPressColor;
}
public void setPressColor(int pressColor) {
this.mPressColor = pressColor;
}
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
refreshShape();
mShape.resize(bounds.right - bounds.left, bounds.bottom - bounds.top);
}
private void refreshShape(){
mOuter = new float[]{mTopLeftRadius, mTopLeftRadius
, mTopRightRadius, mTopRightRadius
, mBottomLeftRadius, mBottomLeftRadius
, mBottomRightRadius, mBottomLeftRadius};
mShape = new RoundRectShape(mOuter, null, null);
}
public void setColor(int color){
mColor = color;
mPaint.setColor(color);
}
@Override
public void draw(Canvas canvas) {
mShape.draw(canvas, mPaint);
}
@Override
public void setAlpha(int alpha) {
mPaint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf) {
mPaint.setColorFilter(cf);
}
@Override
public int getOpacity() {
return mPaint.getAlpha();
}
}
2. java代碼如下:
import android.graphics.Rect;
import android.graphics.drawable.StateListDrawable;
public class StateRoundRectDrawable extends StateListDrawable{
private static final float DEFAULT_RADIUS = 6.f;
private float mTopLeftRadius = DEFAULT_RADIUS;
private float mTopRightRadius = DEFAULT_RADIUS;
private float mBottomLeftRadius = DEFAULT_RADIUS;
private float mBottomRightRadius = DEFAULT_RADIUS;
private int mNormalColor;
private int mPressedColor;
private RoundRectDradable mNormalDradable;
private RoundRectDradable mPressedDradable;
public StateRoundRectDrawable(int normalCorlor, int pressColor) {
this.mNormalColor = normalCorlor;
this.mPressedColor = pressColor;
}
@Override
protected void onBoundsChange(Rect bounds) {
if(mNormalDradable == null){
mNormalDradable = new RoundRectDradable();
mNormalDradable.setTopLeftRadius(mTopLeftRadius);
mNormalDradable.setTopRightRadius(mTopRightRadius);
mNormalDradable.setBottomLeftRadius(mBottomLeftRadius);
mNormalDradable.setBottomRightRadius(mBottomRightRadius);
mNormalDradable.setColor(mNormalColor);
mNormalDradable.onBoundsChange(bounds);
}
if(mPressedDradable == null){
mPressedDradable = new RoundRectDradable();
mPressedDradable.setTopLeftRadius(mTopLeftRadius);
mPressedDradable.setTopRightRadius(mTopRightRadius);
mPressedDradable.setBottomLeftRadius(mBottomLeftRadius);
mPressedDradable.setBottomRightRadius(mBottomRightRadius);
mPressedDradable.setColor(mPressedColor);
mPressedDradable.onBoundsChange(bounds);
}
this.addState(new int[]{-android.R.attr.state_pressed}, mNormalDradable);
this.addState(new int[]{android.R.attr.state_pressed}, mPressedDradable);
}
public float getTopLeftRadius() {
return mTopLeftRadius;
}
public void setTopLeftRadius(float topLeftRadius) {
this.mTopLeftRadius = topLeftRadius;
}
public float getTopRightRadius() {
return mTopRightRadius;
}
public void setTopRightRadius(float topRightRadius) {
this.mTopRightRadius = topRightRadius;
}
public float getBottomLeftRadius() {
return mBottomLeftRadius;
}
public void setBottomLeftRadius(float bottomLeftRadius) {
this.mBottomLeftRadius = bottomLeftRadius;
}
public float getBottomRightRadius() {
return mBottomRightRadius;
}
public void setBottomRightRadius(float bottomRightRadius) {
this.mBottomRightRadius = bottomRightRadius;
}
public int getNormalColor() {
return mNormalColor;
}
public void setNormalColor(int normalColor) {
this.mNormalColor = normalColor;
}
public int getPressedColor() {
return mPressedColor;
}
public void setPressedColor(int pressedColor) {
this.mPressedColor = pressedColor;
}
}
希望本文所述對大家的Android程序設計有所幫助。
Android Studio 打多渠道包
最近,公司的項目開始使用谷歌官方最新推出的IDE——Android Studio 1.0,發現使用studio打渠道包很容易。 下面
Android Build.VERSION.SDK_INT兼容介紹
盡管Android向下兼容不好,但是一個程序還是可以在多個平台上跑的。向下兼容不好,接口改變,新的平台上不能用舊的API,舊的平台更不可能用新的API,不等於一個平台需要
android如何取得本地通訊錄的頭像的原圖
上幾篇講contact的時候,取得的頭像都是存在數據庫中,通過: cursor = context.getContentResolver().query(
Android三句代碼使用沉浸式狀態欄
用過android手機的人都知道android使用app的時候屏幕上方的狀態欄都是黑色的,就算不是黑色的都與正在打開的app顏色不同。有一種灰常不搭調的感覺。