package com.gh.common.view; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.drawable.Drawable; import android.support.v4.content.ContextCompat; import android.util.AttributeSet; import android.widget.RelativeLayout; import com.gh.gamecenter.R; public class CardRelativeLayout extends RelativeLayout { private Drawable mLeftDrawable, mRightDrawable, topDrawable, bottomDrawable; private Drawable leftTopDrawable, rightTopDrawable, leftBottomDrawable, rightBottomDrawable; private int mLeft, mTop, mRight, mBottom; private boolean isTran, isReverse; public CardRelativeLayout(Context context) { this(context, null); } public CardRelativeLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CardRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } private void init(Context context, AttributeSet attrs) { setWillNotDraw(false); if (attrs != null) { TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CardLayout); mLeft = ta.getDimensionPixelSize(R.styleable.CardLayout_left, 0); mTop = ta.getDimensionPixelSize(R.styleable.CardLayout_top, 0); mRight = ta.getDimensionPixelSize(R.styleable.CardLayout_right, 0); mBottom = ta.getDimensionPixelSize(R.styleable.CardLayout_bottom, 0); isTran = ta.getBoolean(R.styleable.CardLayout_tran, false); isReverse = ta.getBoolean(R.styleable.CardLayout_reverse, true); ta.recycle(); } if (isTran) { mLeftDrawable = ContextCompat.getDrawable(context, R.drawable.frame_tran_left); topDrawable = ContextCompat.getDrawable(context, R.drawable.frame_tran_top); mRightDrawable = ContextCompat.getDrawable(context, R.drawable.frame_tran_right); bottomDrawable = ContextCompat.getDrawable(context, R.drawable.frame_tran_bottom); leftTopDrawable = ContextCompat.getDrawable(context, R.drawable.frame_tran_left_top_square); rightTopDrawable = ContextCompat.getDrawable(context, R.drawable.frame_tran_right_top_square); leftBottomDrawable = ContextCompat.getDrawable(context, R.drawable.frame_tran_left_bottom_square); rightBottomDrawable = ContextCompat.getDrawable(context, R.drawable.frame_tran_right_bottom_square); } else { mLeftDrawable = ContextCompat.getDrawable(context, R.drawable.frame_left); topDrawable = ContextCompat.getDrawable(context, R.drawable.frame_top); mRightDrawable = ContextCompat.getDrawable(context, R.drawable.frame_right); bottomDrawable = ContextCompat.getDrawable(context, R.drawable.frame_bottom); leftTopDrawable = ContextCompat.getDrawable(context, R.drawable.frame_left_top_square); rightTopDrawable = ContextCompat.getDrawable(context, R.drawable.frame_right_top_square); leftBottomDrawable = ContextCompat.getDrawable(context, R.drawable.frame_left_bottom_square); rightBottomDrawable = ContextCompat.getDrawable(context, R.drawable.frame_right_bottom_square); } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (!isReverse) { if (mLeft != 0) { if (mLeftDrawable != null) { mLeftDrawable.setBounds(0, mTop, mLeft, getHeight() - mBottom); mLeftDrawable.draw(canvas); } } if (mRight != 0) { if (mRightDrawable != null) { mRightDrawable.setBounds(getWidth() - mRight, mTop, getWidth(), getHeight() - mBottom); mRightDrawable.draw(canvas); } } } if (mTop != 0) { if (isReverse) { if (leftBottomDrawable != null) { leftBottomDrawable.setBounds(0, 0, mLeft, mTop); leftBottomDrawable.draw(canvas); } if (bottomDrawable != null) { bottomDrawable.setBounds(mLeft, 0, getWidth() - mRight, mTop); bottomDrawable.draw(canvas); } if (rightBottomDrawable != null) { rightBottomDrawable.setBounds(getWidth() - mRight, 0, getWidth(), mTop); rightBottomDrawable.draw(canvas); } } else { if (leftTopDrawable != null) { leftTopDrawable.setBounds(0, 0, mLeft, mTop); leftTopDrawable.draw(canvas); } if (topDrawable != null) { topDrawable.setBounds(mLeft, 0, getWidth() - mRight, mTop); topDrawable.draw(canvas); } if (rightTopDrawable != null) { rightTopDrawable.setBounds(getWidth() - mRight, 0, getWidth(), mTop); rightTopDrawable.draw(canvas); } } } if (mBottom != 0) { if (isReverse) { if (leftTopDrawable != null) { leftTopDrawable.setBounds(0, getHeight() - mBottom, mLeft, getHeight()); leftTopDrawable.draw(canvas); } if (topDrawable != null) { topDrawable.setBounds(mLeft, getHeight() - mBottom, getWidth() - mRight, getHeight()); topDrawable.draw(canvas); } if (rightTopDrawable != null) { rightTopDrawable.setBounds(getWidth() - mRight, getHeight() - mBottom, getWidth(), getHeight()); rightTopDrawable.draw(canvas); } } else { if (leftBottomDrawable != null) { leftBottomDrawable.setBounds(0, getHeight() - mBottom, mLeft, getHeight()); leftBottomDrawable.draw(canvas); } if (bottomDrawable != null) { bottomDrawable.setBounds(mLeft, getHeight() - mBottom, getWidth() - mRight, getHeight()); bottomDrawable.draw(canvas); } if (rightBottomDrawable != null) { rightBottomDrawable.setBounds(getWidth() - mRight, getHeight() - mBottom, getWidth(), getHeight()); rightBottomDrawable.draw(canvas); } } } } }