clipDrawable不允许渲染

时间:2016-04-11 17:40:27

标签: android

我的目标是创造一个“温度计”,它以更高的数量增长并以更小的数量收缩。像温度计一样。所以在代码中我设计了一个可绘制的位图并将其传递给clipDrawable。图像完全呈现,直到我将其传递给剪辑drawable,然后我什么都没得到。

我的主要:

public class MainActivity extends AppCompatActivity {

    private TestView testView;
    private Timer timer;
    private int delay = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        testView = (TestView) findViewById(R.id.test_view);
        timer = new Timer();

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                testView.update();
            }
        }, 0, delay);
    }
}

我的观点类:

public class TestView extends View {

    private Paint rectPaint;
    private Paint barPaint;
    private Rect coverRect;
    private int x;
    private int y;
    private int x2;
    private int y2;
    private int SIZE = 200;

    //Defaults
    private int def_background = Color.BLACK;

    private Resources res;
    private Bitmap colorbarMap;
    private BitmapDrawable colorbarDraw;
    private ClipDrawable colorbarClip;
    private ViewTreeObserver viewTreeObserver;

    private int max = 100;
    private int min = 0;
    private int view_height;
    private int view_width;
    private boolean startup = true;

    //Customizable
    private int set_background;

    public TestView(Context context, AttributeSet attrs) {
        super(context, attrs);
        readAttrs(context, attrs, 0);
        init();
    }

    public TestView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        readAttrs(context, attrs, defStyleAttr);
        init();
    }

    public TestView(Context context) {
        super(context);
        readAttrs(context, null, 0);
        init();
    }

    private void init(){
        //System
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB) {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        res = getResources();
        viewTreeObserver = getViewTreeObserver();
        if (viewTreeObserver.isAlive()) {
            viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    view_width = getWidth();
                    view_height = getHeight();
                    afterLayout();
                }
            });
        }

        //Paints
        rectPaint = new Paint();
        barPaint = new Paint();

        //Shapes
        coverRect = new Rect();

        //Ints-floats-doubles
        x = 0;
        y = 0;
        x2 = 100;
        y2 = 100;

        rectPaint.setColor(set_background);
        rectPaint.setStyle(Paint.Style.FILL);
        coverRect.set(x, y, x2, y2);
    }

    public void update(){
        if(!startup) colorbarClip.invalidateSelf();
        postInvalidate();
    }

    private void afterLayout(){
        //Images - Create resource directory object, use it to set an image to a bitmap
        //  object using bitmap factory and scale it. Then create a drawable using that bitmap.
        //  Set the bounds of the new drawable bitmap. Assign it to a clip object for partial rendering
        colorbarMap = BitmapFactory.decodeResource(res, R.drawable.colorbar);
        colorbarMap = Bitmap.createScaledBitmap(colorbarMap, view_width, view_height, true);
        colorbarDraw = new BitmapDrawable(res, colorbarMap);
        colorbarDraw.setBounds(0, 0, view_width, view_height);
        colorbarClip = new ClipDrawable(colorbarDraw, Gravity.BOTTOM, ClipDrawable.VERTICAL);
        colorbarClip.setLevel(10000);

        startup = false;
    }

    @Override
    protected void onDraw(Canvas canvas){
        canvas.drawColor(set_background);
        if(!startup) colorbarClip.draw(canvas);
    }

    private void readAttrs(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TestView, defStyleAttr, 0);
            set_background = a.getInteger(R.styleable.TestView_set_background, def_background);
        a.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int chosenWidth = chooseDimension(widthMode, widthSize);
        int chosenHeight = chooseDimension(heightMode, heightSize);
        setMeasuredDimension(chosenWidth, chosenHeight);
    }

    private int chooseDimension(int mode, final int size) {
        switch (mode) {
            case MeasureSpec.AT_MOST:
            case MeasureSpec.EXACTLY:
                return size;
            case MeasureSpec.UNSPECIFIED:
            default:
                return SIZE;
        }
    }

    @Override
    protected void onRestoreInstanceState(final Parcelable state) {
        Bundle bundle = (Bundle) state;
        Parcelable superState = bundle.getParcelable("superState");
        super.onRestoreInstanceState(superState);

        x2 = bundle.getInt("x2");
        y2 = bundle.getInt("y2");
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        Parcelable superState = super.onSaveInstanceState();
        Bundle state = new Bundle();
        state.putParcelable("superState", superState);

        state.putInt("x2",x2);
        state.putInt("y2",y2);

        return state;
    }
}

我的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"
    xmlns:testview="http://schemas.android.com/apk/res-auto"
    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"
    tools:context="prospect_industries.viewobject.MainActivity">

    <prospect_industries.viewobject.TestView
        android:id="@+id/test_view"
        android:layout_width="60dp"
        android:layout_height="400dp"
        android:layout_centerVertical="true"
        testview:set_background="-16776961" />

</RelativeLayout>

当我说在来到这里之前我真的尽力找到问题的解决方案时,请相信我。我无法弄清楚我可能会遗漏什么或者我可能不理解的概念。

1 个答案:

答案 0 :(得分:0)

由于没有人回答,我会为未来的路人发布答案。我最终搞清楚我做错了什么。 BitmapDrawable和ClipDrawable具有必须在绘制图像之前设置的边界。如果没有正确完成,那么你将无法渲染。

相关问题