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程序设计有所帮助。

(0)

相关推荐

  • android 设置圆角图片实现代码

    复制代码 代码如下: <?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_wi

  • android 实现圆角图片解决方案

    现在我们就来看看怎么样把图片的四角都变成圆形的,为什么要这样做那,如果要是这样界面就会非常的美观,下面我们就来看看代码吧. java代码: 复制代码 代码如下: public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canv

  • Android图片特效:黑白特效、圆角效果、高斯模糊

    1.黑白效果 复制代码 代码如下: /**     * 将彩色图转换为黑白图     *      * @param 位图     * @return 返回转换好的位图     */    public static Bitmap convertToBlackWhite(Bitmap bmp) {        int width = bmp.getWidth(); // 获取位图的宽        int height = bmp.getHeight(); // 获取位图的高 int[] pi

  • Android自定义Drawable实现圆形和圆角

    本文实例为大家分享了自定义Drawable实现圆形和圆角的具体代码,供大家参考,具体内容如下 圆形 package com.customview.widget; import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint;

  • Android中实现EditText圆角的方法

    一.在drawable下面添加xml文件rounded_editview.xml 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <solid andr

  • Android裁剪图片为圆形图片的实现原理与代码

    以前在eoe论坛中找过裁剪图片为圆形图片的方法,但是效果都不是很理想,这几天因为公司业务的要求,需要对头像进行裁剪以圆形的方式显示,这个方法是根据传入的图片的高度(height)和宽度(width)决定的,如果是 width <= height时,则会裁剪高度,裁剪的区域是宽度不变高度从顶部到宽度width的长度:如果 width > height,则会裁剪宽度,裁剪的区域是高度不变,宽度是取的图片宽度的中心区域,不过不同的业务需求,对裁剪图片要求不一样,可以根据业务的需求来调整裁剪的区域.

  • Android使用CircleImageView实现圆形头像的方法

    有时我们在应用中会用到圆形头像,下面是利用CircleImageView实现圆形头像的演示,下面效果和代码,效果如图 实现起来也比较简单,先在项目中建一个circleimageview包用来存放CircleImageView类,待会直接把CircleImageView类复制到包里就可以使用了 然后,再建一个attrs.xml,其代码相当简单,定义了圆形边框宽度和颜色 <?xml version="1.0" encoding="utf-8"?> <r

  • Android关于Glide的使用(高斯模糊、加载监听、圆角图片)

    高斯模糊.加载监听.圆角图片这些相信大家都很熟悉,那如何实现这些效果,请大家参考本文进行学习. 1.引用 compile 'com.github.bumptech.glide:glide:3.7.0' 2.加载图片 2.1 基本加载 Glide.with(context)     .load(url)     .into(imageView); 2.2 设置加载中和加载失败的情况 Glide.with(context) .load(url) .placeholder(R.drawable.loa

  • Android实现本地上传图片并设置为圆形头像

    先从本地把图片上传到服务器,然后根据URL把头像处理成圆形头像. 因为上传图片用到bmob的平台,所以要到bmob(http://www.bmob.cn)申请密钥. 效果图: 核心代码: 复制代码 代码如下: public class MainActivity extends Activity {         private ImageView iv;         private String appKey="";                //填写你的Applicatio

  • Android实现圆角矩形和圆形ImageView的方式

    Android中实现圆角矩形和圆形有很多种方式,其中最常见的方法有ImageLoader设置Option和自定义View. 1.ImageLoader加载图片 public static DisplayImageOptions getRoundOptions() { DisplayImageOptions options = new DisplayImageOptions.Builder() // 是否设置为圆角,弧度为多少,当弧度为90时显示的是一个圆 .displayer(new Round

随机推荐