当前位置: 移动技术网 > 移动技术>移动开发>Android > android实现圆角矩形背景的方法

android实现圆角矩形背景的方法

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例讲述了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程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网