/** Inits the. */ private void init() { mResizeDrawableDiagonal = AbImageUtil.bitmapToDrawable(AbFileUtil.getBitmapFormSrc("image/crop_big.png")); mResizeDrawableDiagonal2 = AbImageUtil.bitmapToDrawable(AbFileUtil.getBitmapFormSrc("image/crop_small.png")); }
/** * Instantiates a new ab slider button. * * @param context the context * @param attrs the attrs */ public AbSlidingButton(Context context, AttributeSet attrs) { super(context, attrs); btnWidth = 40; btnHeight = 40; // 移动的距离 moveWidth = 45; // 每次移动的距离 movePDis = 5; // 覆盖按钮的宽 WidthOffset = 5; WindowManager wManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wManager.getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); /* 2 px = 3 dip if dpi == 80(ldpi), 320x240 screen 1 px = 1 dip if dpi == 160(mdpi), 480x320 screen 3 px = 2 dip if dpi == 240(hdpi), 840x480 screen */ if (width < 320) { btnWidth = btnWidth - 10; btnHeight = btnHeight - 10; moveWidth = moveWidth - 10; } else if (width > 320 && width < 450) { btnWidth = btnWidth + 10; btnHeight = btnHeight + 10; moveWidth = moveWidth + 10; } else if (width >= 450) { btnWidth = btnWidth + 20; btnHeight = btnHeight + 20; moveWidth = moveWidth + 20; } detector = new GestureDetector(this); mRelativeLayout = new RelativeLayout(context); ImageButton btnOn = new ImageButton(context); ImageButton btnOff = new ImageButton(context); btnLeft = new ImageButton(context); btnRight = new ImageButton(context); btnOn.setFocusable(false); btnOff.setFocusable(false); btnLeft.setFocusable(false); btnRight.setFocusable(false); Bitmap onImage = AbFileUtil.getBitmapFormSrc("image/button_on_bg.png"); Bitmap offImage = AbFileUtil.getBitmapFormSrc("image/button_off_bg.png"); Bitmap blockImage = AbFileUtil.getBitmapFormSrc("image/button_block.png"); btnOn.setId(ID_BTN1); // btnOn.setImageBitmap(onImage); btnOn.setMinimumWidth(btnWidth); btnOn.setMinimumHeight(btnHeight); btnOn.setBackgroundDrawable(AbImageUtil.bitmapToDrawable(onImage)); btnOff.setId(ID_BTN2); btnOff.setMinimumWidth(btnWidth); btnOff.setMinimumHeight(btnHeight); // btnOff.setImageBitmap(offImage); btnOff.setBackgroundDrawable(AbImageUtil.bitmapToDrawable(offImage)); btnLeft.setId(ID_BTN3); btnLeft.setMinimumWidth(btnWidth + WidthOffset); btnLeft.setMinimumHeight(btnHeight); // btnLeft.setImageBitmap(blockImage); btnLeft.setBackgroundDrawable(AbImageUtil.bitmapToDrawable(blockImage)); btnRight.setId(ID_BTN4); btnRight.setMinimumWidth(btnWidth + WidthOffset); btnRight.setMinimumHeight(btnHeight); // btnRight.setImageBitmap(blockImage); btnRight.setBackgroundDrawable(AbImageUtil.bitmapToDrawable(blockImage)); mRelativeLayout.addView(btnOn); mRelativeLayout.setFocusable(false); RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp2.addRule(RelativeLayout.RIGHT_OF, ID_BTN1); mRelativeLayout.addView(btnOff, lp2); RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp3.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); mRelativeLayout.addView(btnLeft, lp3); RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp4.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); mRelativeLayout.addView(btnRight, lp4); addView(mRelativeLayout); mSwitcherChangeListener = new AbOnChangeListener() { @Override public void onChange(int position) {} }; }