Created
June 12, 2015 03:34
-
-
Save extralam/5a244bb1ae97eadbc3d7 to your computer and use it in GitHub Desktop.
Make a Rounded RelativeLayout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.goxip.app.ui; | |
import android.annotation.SuppressLint; | |
import android.annotation.TargetApi; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Paint; | |
import android.graphics.Path; | |
import android.graphics.RectF; | |
import android.os.Build; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.widget.LinearLayout; | |
import android.widget.RelativeLayout; | |
import com.goxip.app.R; | |
/** | |
* Created by Alan on 13/5/2014. | |
*/ | |
public class RoundedRelativeLayout extends RelativeLayout { | |
/** Used locally to tag Logs */ | |
@SuppressWarnings("unused") | |
private static final String TAG = RoundedRelativeLayout.class.getSimpleName(); | |
private final Path mPath = new Path(); | |
private Paint paint = new Paint(); | |
@TargetApi(Build.VERSION_CODES.HONEYCOMB) | |
public RoundedRelativeLayout(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(); | |
} | |
public RoundedRelativeLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public RoundedRelativeLayout(Context context) { | |
super(context); | |
init(); | |
} | |
@SuppressLint("NewApi") | |
private void init() { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { | |
setLayerType(View.LAYER_TYPE_SOFTWARE, null); | |
} | |
paint.setAntiAlias(true); | |
} | |
@Override | |
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | |
super.onSizeChanged(w, h, oldw, oldh); | |
mPath.reset(); | |
float round = getResources().getDimension(R.dimen.news_big_padding); | |
mPath.addRoundRect(new RectF(getPaddingLeft(), getPaddingTop(), w - getPaddingRight(), h - getPaddingBottom()), round, round, Path.Direction.CW); | |
} | |
@Override | |
protected void dispatchDraw(Canvas canvas) { | |
canvas.clipPath(mPath); | |
super.dispatchDraw(canvas); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment