Mein Ziel ist es, ein "Thermometer" zu erstellen, das bei einer höheren Zahl wächst und bei einer kleineren Zahl schrumpft. Wie ein Thermometer. Also entwerfe ich im Code ein Bitmap-Zeichen und übergebe es an clipDrawable. Das Bild rendert einwandfrei bis ich es über Clip Drawable übergebe, dann bekomme ich nichts.clipDrawable wird kein Rendering erlauben
mein Haupt:
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);
}
}
Meine Ansicht Klasse:
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;
}
}
Meine 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>
mir vertrauen, wenn ich sage, ich wirklich mein Bestes zu versuchen, getan haben, zu finden eine Lösung für mein Problem, bevor Sie hierher kommen. Ich kann nicht herausfinden, was ich vermisse oder welches Konzept ich vielleicht nicht verstehe.