Android clipDrawable不允许渲染
我的目标是创造一个“温度计”,它以较高的数值增长,以较小的数值收缩。像温度计一样。所以在代码中,我设计了一个位图drawable并将其传递给clipDrawable。图像呈现得非常完美,直到我将其传递给clip drawable,然后我什么也得不到 我的主要意见是:Android clipDrawable不允许渲染,android,Android,我的目标是创造一个“温度计”,它以较高的数值增长,以较小的数值收缩。像温度计一样。所以在代码中,我设计了一个位图drawable并将其传递给clipDrawable。图像呈现得非常完美,直到我将其传递给clip drawable,然后我什么也得不到 我的主要意见是: public class MainActivity extends AppCompatActivity { private TestView testView; private Timer timer; p
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;
}
}
公共类TestView扩展了视图{
私人涂料;
私人油漆;
私人Rect-coverRect;
私人INTX;
私营企业;
私人int x2;
私人互联网y2;
私有整数大小=200;
//默认值
private int def_background=Color.BLACK;
私人资源;
私有位图彩色条码;
私有位图可绘制colorbarDraw;
私人剪贴画彩色剪贴画;
私有ViewTreeObserver ViewTreeObserver;
私人int最大值=100;
私有int min=0;
私家车内窥镜高度;
私有int视图的宽度;
私有布尔启动=true;
//可定制
私人int set_背景;
公共测试视图(上下文、属性集属性){
超级(上下文,attrs);
readAttrs(上下文,attrs,0);
init();
}
公共测试视图(上下文上下文、属性集属性、int-defStyleAttr){
super(上下文、attrs、defStyleAttr);
readAttrs(上下文、attrs、defStyleAttr);
init();
}
公共测试视图(上下文){
超级(上下文);
readAttrs(上下文,null,0);
init();
}
私有void init(){
//系统
如果(Build.VERSION.SDK_INT因为没有人回答,我将为未来的路人发布一个答案。我最终发现我做错了什么。BitmapDrawable和ClipDrawable有一个界限,在绘制图像之前必须同时设置这两个界限。如果做得不正确,那么将无法进行渲染
<?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>