如何在Android中创建动态颜色指示器?

时间:2015-12-14 12:51:17

标签: java android android-custom-view android-color

我想创建一个动态指示灯,我想设置颜色。我创建了一个自定义组件扩展视图。我手动放Color.RED。但我想从我的Activity类中设置颜色。请帮我做这个朋友

enter image description here

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class IndicatorView extends View {

private Paint paint;
private Paint paintBlur;
private int iColor;

public IndicatorView(Context context,int mColor) {
    super(context);
    this.iColor = mColor;
    init();
}

public IndicatorView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public IndicatorView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

private void init() {
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setDither(true);
    paint.setColor(iColor);
    paint.setStrokeWidth(20f);
    paint.setStyle(Paint.Style.FILL);

    paintBlur = new Paint();
    paintBlur.set(paint);
    paintBlur.setColor(iColor);
    paintBlur.setStrokeWidth(30f);
    paintBlur.setMaskFilter(new BlurMaskFilter(15, BlurMaskFilter.Blur.SOLID));
}

@Override
protected void onDraw(Canvas canvas) {

    final int width = getWidth() - getPaddingLeft() - getPaddingRight();
    final int height = getHeight() - getPaddingTop() - getPaddingBottom();

    final int cx = width / 2;
    final int cy = height / 2;

    final float diameter = Math.min(width,height) - paint.getStrokeWidth();
    final float radius = diameter / 2;

    canvas.drawCircle(cx,cy,radius,paint);
    canvas.drawCircle(cx,cy,radius,paintBlur);
}

public int getmColor() {
    return iColor;
}

public void setmColor(int iColor) {
    this.iColor = iColor;
}
}

我的活动:

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    IndicatorView indicatorView = new IndicatorView(this);

    /*IndicatorView myIndicator = (IndicatorView) findViewById(R.id.myIndicator);
    myIndicator.setmColor(R.color.colorAccent);*/

}
}

活动XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#FF000000"
tools:context="com.sample.mysampleapp.MainActivity">

<com.sample.mysampleapp.IndicatorView
    android:id="@+id/myIndicator"
    android:layout_width="50dp"
    android:layout_height="50dp" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

在绘画对象上设置颜色后,需要使视图无效。

public void setmColor(@ColorInt int iColor) {
    this.iColor = iColor;
    paint.setColor(iColor);
    invalidate();
}

然后在您的活动中调用此方法:

indicatorView.setmColor(ContextCompat.getColor(this, R.color.colorAccent));