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

How to create a dynamic colour Indicator in Android?

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

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;
}
}

我的Activity:

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);*/

}
}

Activity 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>

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

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

然后在你的activity中调用这个方法:

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