如何以编程方式创建或更改由不同颜色的线的绘制 [英] How to programmatically create or alter a drawable made of lines of different colors

查看:135
本文介绍了如何以编程方式创建或更改由不同颜色的线的绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要画一个3DP线重新present在QUIZZ游戏的水平完成。

I have to draw a 3dp line to represent a level completion in a quizz game.

这行必须是2种颜色。例如,如果用户已经完成的水平的40%,该线将是红色的线的第一个40%,另外60%是灰色。

This line must be of 2 colors. For example, if user has completed 40% of the level, the line will be red for the first 40% of the line, the other 60% being grey.

我已经成功地做​​到这一点的一个绘制:

I have managed to do that with a drawable :

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="line" >
            <size android:height="3dp" android:width="40dp"/>
            <stroke
                android:width="1dp"
                android:color="#FFFC10" />
        </shape>
    </item>
    <item android:left="40dp">
        <shape android:shape="line" >
            <size android:height="3dp" android:width="60dp" />
            <stroke
                android:width="1dp"
                android:color="#DDDDDD" />
        </shape>
    </item>
</layer-list>

然后我用ImageView的显示它:

And then I display it with an ImageView :

<ImageView
                android:id="@+id/row_completion_bar"
                android:src="@drawable/completion_bar"
                android:layout_width="100dp"
                android:layout_height="3dp" />

......但现在,我当然必须能够改变这种40%/ 60%,比取决于actuel用户完成。

... but now, I must of course be able to change this 40%/60% ration depending of the actuel user completion.

第一个问题:什么是最好的最有效的方式做到这一点?更改绘制在运行时?或用Java在运行时创建一个新的绘制?

First question: what is the best most efficient way to do it ? Change the drawable at runtime ? or create a new drawable at runtime in Java ?

第二个问题,怎么办呢?我想两种方式(重新创建绘制在java中code /修改XML绘制在运行时),并没有成功: - (

Second question: how to do it ? I tried both ways (recreate this drawable in java code / alter the xml drawable at runtime) and didn't succeeded :-(

任何帮助将大大AP preciated。

Any help will be greatly appreciated.

推荐答案

所以这是一个自定义绘制的,你可以使用:

so this is a custom Drawable you can use:

class LineDrawable extends Drawable {
    private Paint mPaint;

    public LineDrawable() {
        mPaint = new Paint();
        mPaint.setStrokeWidth(3);
    }

    @Override
    public void draw(Canvas canvas) {
        int lvl = getLevel();
        Rect b = getBounds();
        float x = b.width() * lvl / 10000.0f;
        float y = (b.height() - mPaint.getStrokeWidth()) / 2;
        mPaint.setColor(0xffff0000);
        canvas.drawLine(0, y, x, y, mPaint);
        mPaint.setColor(0xff00ff00);
        canvas.drawLine(x, y, b.width(), y, mPaint);
    }

    @Override
    protected boolean onLevelChange(int level) {
        invalidateSelf();
        return true;
    }

    @Override
    public void setAlpha(int alpha) {
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}

和测试code:

View v = new View(this);
final LineDrawable d = new LineDrawable();
d.setLevel(4000);
v.setBackgroundDrawable(d);
setContentView(v);
OnTouchListener l = new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int lvl = (int) (10000 * event.getX() / v.getWidth());
        d.setLevel(lvl);
        return true;
    }
};
v.setOnTouchListener(l);

这篇关于如何以编程方式创建或更改由不同颜色的线的绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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