2016-04-11 9 views
0

私の目標は、より高い数値で成長し、小さな数値で縮小する「温度計」を作成することです。温度計のように。だからコードでは、私はビットマップのdrawableを設計し、clipDrawableに渡します。私はそれをclip drawableに渡すまで、イメージは完全に正しくレンダリングされます。そして、私は何も取得しません。clipDrawableレンダリングができません

私のメイン:

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> 

私は私は本当に見つけることを試みで私の最善を尽くしていると言うとき私を信頼ここに来る前に私の問題の解決策。私は何が欠けているか、私が理解できないかもしれない概念を理解することはできません。

答えて

0

誰も答えなかったので、私は将来の通行人のための答えを掲示します。私は私が間違っていることを理解することに終わった。 BitmapDrawableとClipDrawableには、イメージを描画する前に両方を設定する必要がある境界があります。それが正しく行われていない場合、レンダリングは行われません。

関連する問題