Я хочу создать динамический индикатор, я хочу установить цвет. Я создал пользовательский компонент, расширяющий представление. Я вручную поставить Color.RED
.Но я хочу, чтобы установить цвет от моей деятельности class.Please помочь мне сделать это друзейКак создать индикатор динамического цвета в Android?
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;
}
}
Моя активность:
активность 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>
В 'SetColor()' метод вы просто изменить переменную цвета, вы должны перерисовать компонент. – Gonzalo
@ Gonzalo bro вы можете помочь мне перерисовать код. – reegan29
public IndicatorView (контекстный контекст, цвет int) { супер (контекст); init(); } // передаем цвет, подобный этому, и установите –