如何改变Android的恒星的侧视图的颜色吗? [英] How to change the color of the side view of the star in android?

查看:166
本文介绍了如何改变Android的恒星的侧视图的颜色吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要看到下面的图片,

掩蔽星级

而code是,

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Region;
import android.view.View;
import android.graphics.Bitmap;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;

/**
 * Created by chozarajan.pandiyarajan on 11/3/2015.
*/

public class SfRatingItem extends View {

private int fillColor, minDim, topXPoint, topYPoint;
private double bigHypot, bigA, bigB, littleHypot, littleA, littleB, value;
private Paint starPaint;
private Path path;
private Bitmap starBitmap;
private Bitmap backBitmap;

public SfRatingItem(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialize();
}

public SfRatingItem(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initialize();
}

public SfRatingItem(Context context) {
    super(context);
    initialize();
    this.setBackgroundColor(Color.GREEN);

}

@Override
protected void onDraw(Canvas canvas) {
    initialDraw();
    Paint q = new Paint(Paint.ANTI_ALIAS_FLAG);

    //canvas.saveLayer(0,0,canvas.getWidth(),canvas.getHeight(),q); // expensive call, instead set a hardware layer
    setLayerType(LAYER_TYPE_HARDWARE, q);

    canvas.drawBitmap(backBitmap, 0, 0, q);
    q.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    canvas.drawBitmap(starBitmap, 0, 0, q);
    q.setXfermode(null);

    Paint p=new Paint();
    p.setColor(Color.RED);
    p.setStyle(Paint.Style.STROKE);
    p.setAntiAlias(true);
    canvas.drawPath(path,p);

}

private void initialDraw() {
    starPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    starPaint.setAntiAlias(true);

    minDim = Math.min(this.getWidth() - this.getPaddingLeft() - this.getPaddingRight(), this.getHeight() - this.getPaddingTop() - this.getPaddingBottom());

    bigHypot = (minDim / Math.cos(Math.toRadians(18)));
    bigB = minDim;
    bigA = Math.tan(Math.toRadians(18)) * bigB;

    littleHypot = bigHypot / (2 + Math.cos(Math.toRadians(72)) + Math.cos(Math.toRadians(72)));
    littleA = Math.cos(Math.toRadians(72)) * littleHypot;
    littleB = Math.sin(Math.toRadians(72)) * littleHypot;

    topXPoint = (this.getWidth() - this.getPaddingLeft() - this.getPaddingRight()) / 2;
    topYPoint = this.getPaddingTop();

    path.moveTo(topXPoint, topYPoint);
    path.lineTo((int) (topXPoint + bigA), (int) (topYPoint + bigB));
    path.lineTo((int) (topXPoint - littleA - littleB), (int) (topYPoint + littleB));
    path.lineTo((int) (topXPoint + littleA + littleB), (int) (topYPoint + littleB));
    path.lineTo((int) (topXPoint - bigA), (int) (topYPoint + bigB));
    path.lineTo(topXPoint, topYPoint);
    path.close();

    RectF bounds = new RectF();
    path.computeBounds(bounds, true);

    // Draw the background rectangle in a bitmap
    starPaint.setColor(Color.RED);
    backBitmap = Bitmap.createBitmap((int) bounds.width(), (int) bounds.height(), Bitmap.Config.ARGB_8888);
    Canvas backCanvas = new Canvas(backBitmap);
    final Rect backRect = new Rect(0, 0, 200, backBitmap.getHeight());

    // Draw the STAR mask in a bitmap

    starBitmap = Bitmap.createBitmap((int) bounds.width(), (int) bounds.height(), Bitmap.Config.ARGB_8888);
    Canvas starCanvas = new Canvas(starBitmap);
    starPaint.setColor(Color.RED);
    starCanvas.drawPath(path, starPaint);
    backCanvas.drawRect(backRect, starPaint);
}

private void initialize() {
    starPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    path = new Path();
 }
}

请看到上述的图像。

在此code我正在设置窗口的背景是绿色。

In this code i am set the background of the window is green color.

和我的问题是,为什么恒星的外面呈现黑色?以及如何改变颜色?

And my question is, why the outside of the star is showing black color? and how to change that color?

推荐答案

这是不工作的原因是因为硬件加速可能会被关闭,您需要打开它在。请参阅本: http://developer.android.com/guide/topics/显卡/硬件accel.html

The reason this is not working is because the Hardware acceleration might be turned off and you need to turn it on. Refer this: http://developer.android.com/guide/topics/graphics/hardware-accel.html

但是,如果你不打算做这个通过硬件加速,然后用

But if you are not planning to do this via Hardware Acceleration, then use

canvas.saveLayer(0,0,canvas.getWidth(),canvas.getHeight(),q);

而不是这样的:

setLayerType(LAYER_TYPE_HARDWARE, q);

这篇关于如何改变Android的恒星的侧视图的颜色吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆