具有渐变背景的自定义视图寻呼机

时间:2016-03-06 14:32:03

标签: android android-fragments android-viewpager gradient android-tabs

我们如何创建自定义渐变背景视图寻呼机,如及时应用。 我能够像下面一样创建径向渐变 在我的标签片段xml

<view
android:id="@+id/myView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
class="com.android.MyView" />

MyView.java

   public class MyView extends View {

Paint BackPaint = new Paint();
Context MyContext;

public MyView(Context context) {
    super(context);
    init(context);
}

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

public MyView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
}

private void init(Context ctx) {
    MyContext = ctx;
    BackPaint.setStyle(Paint.Style.FILL);
    BackPaint.setColor(Color.BLACK);

}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int w = MeasureSpec.getSize(widthMeasureSpec);
    int h = MeasureSpec.getSize(heightMeasureSpec);
    setMeasuredDimension(w, h);
}

@Override
protected void onDraw(Canvas canvas) {

    float w, h, cx, cy, radius;
    w = getWidth();
    h = getHeight();
    cx = w / 2;
    cy = h / 2;

    if (w > h) {
        radius = h / 4;
    } else {
        radius = w / 4;
    }

    canvas.drawRect(0, 0, w, h, BackPaint);

    Paint MyPaint = new Paint();
    MyPaint.setStyle(Paint.Style.FILL);
    SharedPreferences prefs;
    prefs = PreferenceManager.getDefaultSharedPreferences(getContext());

    float shaderCx = 0;
    float shaderCy = 0;

    float shaderRadius = w;

    int shaderColor0 = Color.WHITE;
    int shaderColor1 = Color.BLACK;
    shaderColor0 = prefs.getInt("pref1", 0);
    shaderColor1 = prefs.getInt("pref2", 0);
    MyPaint.setAntiAlias(true);
    Shader radialGradientShader;

    radialGradientShader = new RadialGradient(shaderCx, shaderCy,
            shaderRadius, shaderColor0, shaderColor1,
            Shader.TileMode.MIRROR);

    MyPaint.setShader(radialGradientShader);
    canvas.drawRect(0, 0, w, h, MyPaint);
    // canvas.drawRect(0, 0, w, h, MyPaint);

    // canvas.drawCircle(cx, cy, radius, MyPaint);
};

}

但是我们如何为每个标签创建单独的渐变?

0 个答案:

没有答案