在自定义视图重复中拉出过多的ram

时间:2016-07-02 09:35:56

标签: android performance android-custom-view

我正在为剧院的座位设计一个customview。对于x大型手机来说,它运行顺畅。但在低密度设备中,它很难配合使用。 我的customview.java

public class SeatView extends ImageView {
    float dim;
    int status;
    Canvas canvas1;
    boolean sela=false,ds=false,ua=false;
    Bitmap selectedAvailableImage,deselectedAvailableImage,unavailableImage;
    Drawable selectedAvailable,deselectedAvailable,unavailable;
    SeatView seatView;

    public SeatView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs,R.styleable.SeatView,0, 0);

        try {

            dim = a.getDimension(R.styleable.SeatView_dim, 20f);
            status=a.getInt(R.styleable.SeatView_status,1);
            selectedAvailableImage=BitmapFactory.decodeResource(getResources(),R.drawable.seat_selected);
            deselectedAvailableImage=BitmapFactory.decodeResource(getResources(),R.drawable.seat_available);
            unavailableImage=BitmapFactory.decodeResource(getResources(),R.drawable.seat_unavailable);
            selectedAvailable=getResources().getDrawable(R.drawable.seat_selected);
            deselectedAvailable=getResources().getDrawable(R.drawable.seat_available);
            unavailable=getResources().getDrawable(R.drawable.seat_unavailable);
            //selectedAvailable.setBounds(100,100,100,100);
            //deselectedAvailable.setBounds(10,10,10,10);
            //unavailable.setBounds(10,10,10,10);

        } finally {
            a.recycle();
        }



    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if(event.getAction()==MotionEvent.ACTION_UP){
            if(status==0){
                status=1;
                invalidate();
            }else if(status==1){
                status=0;
                invalidate();
            }
        }
        return true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        setMeasuredDimension(50,50);
    }

    public void setAvailable(){

        status=1;
        invalidate();


      }
    public void setDisabled(){

        status=2;
        invalidate();


    }
    public void setSelected(){

        status=0;
        invalidate();

        }
    public int getSeatStatus(){
        return status;
    }
    public void init(Canvas canvas){
        switch (status){
            case 0:
                //sela=true;ds=false;ua=false;

                //setImageDrawable(selectedAvailable);
                canvas.drawBitmap(selectedAvailableImage,0,0,null);

                break;
            case 1:
                //sela=false;ds=true;ua=false;

                //setImageDrawable(deselectedAvailable);

                canvas.drawBitmap(deselectedAvailableImage,0,0,null);
                break;
            case 2:
                //sela=false;ds=false;ua=true;

                //setImageDrawable(unavailable);
                canvas.drawBitmap(unavailableImage,0,0,null);

                break;
            default: //setImageDrawable(deselectedAvailable);
                canvas.drawBitmap(deselectedAvailableImage,0,0,null);

                break;
        }

    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        init(canvas);
    }
}

和我的mainactivity.java

public class MainActivity extends AppCompatActivity {

ImageView imageView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        //ButterKnife.bind(this);
        TableLayout tableLayout=(TableLayout)findViewById(R.id.tblelay);
       // Toast.makeText(getApplicationContext(),seatView.getSeatStatus()+"",Toast.LENGTH_LONG).show();
        //LinearLayout relativeLayout=(LinearLayout) findViewById(R.id.tblelay);
        for (int j=0;j<20;j++){
        TableRow tableRow=new TableRow(this);

        for (int i=1;i<20;i++){
            SeatView seatView=new SeatView(this,null);
            seatView.setAvailable();
            seatView.setId(i);
            if(i%5==0){
                seatView.setVisibility(View.INVISIBLE);

            }
            if(seatView.getParent()!=null)
                ((ViewGroup)seatView.getParent()).removeView(seatView); // <- fix
            if(tableRow.getParent()!=null)
                ((ViewGroup)tableRow.getParent()).removeView(tableRow);
           /* if(i>0){
                RelativeLayout.LayoutParams layoutParams=new Rela   tiveLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                layoutParams.addRule(RelativeLayout.RIGHT_OF, (i-1));
            }*/
            tableRow.addView(seatView);
        }
        tableLayout.addView(tableRow);}

    }


}

点击here获取屏幕截图

0 个答案:

没有答案