Android - Background transparency sets also transparency on Paint object - android

Im creating a custom view (extending Linearlayout) which has as background color: #B3000000 (75% transparency = B3), and now I need to draw something on this view with full color and not with B3/75% transparency.
What I currently have:
public class MyCustomObject extends LinearLayout {
private Paint paint;
public MyCustomObject(Context context) {
super(context);
init();
}
public MyCustomObject(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyCustomObject(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MyCustomObject(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
View v = View.inflate(getContext(), R.layout.custom_view, this);
paint = new Paint();
paint.setColor(Color.RED);
paint.setAlpha(255);
paint.setStrokeWidth(50);
setWillNotDraw(false);
}
#Override
protected void onDraw(Canvas canvas) {
// super.onDraw(canvas);
canvas.drawLine(0,0,getWidth(),getHeight(),paint);
Timber.e("Drawing...");
}
}
I would like to draw a RED line on top of this view, and at this moment it draw a red line but with a "black layer" on top, instead a vibrante RED color.

I found the solution here
#Override
protected void dispatchDraw(Canvas canvas) {
// draw here if you want to draw behind children
super.dispatchDraw(canvas);
// draw here if you want to draw over children
}

Related

why I can't use path draw a line,but I use drawline method it's ok?

I use canvas.drawPath can't draw a line,why? but if use the canvas.drawLine() method,it's ok,this is my code.
public class TestView extends View {
public TestView(Context context) {
super(context);
}
public TestView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
}
public TestView(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected void onDraw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.parseColor("#000000"));
//canvas.drawLine(0,0,100,100,p);
Path path = new Path();
path.moveTo(0,0);
path.lineTo(100,100);
canvas.drawPath(path,p);
}
}
who can tell me why? thanks advanced

Android onDraw never called

I have a custom linear layout from which I extend. I have already called setWIllNotCacheDrawing but it wont work. Here is my Class:
public class ClippedLinearLayout extends LinearLayout {
public ClippedLinearLayout(Context context) {
super(context);
this.setWillNotCacheDrawing(false);
}
public ClippedLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.setWillNotCacheDrawing(false);
}
public ClippedLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.setWillNotCacheDrawing(false);
}
#Override
protected void onDraw(Canvas canvas) {
Log.e("clipped?", "clipped?);
Path mPath = new Path();
mPath.addCircle(50, 50, 50, Path.Direction.CCW);
canvas.clipPath(mPath, Region.Op.INTERSECT);
super.onDraw(canvas);
}
}
You're extending a ViewGroup, and sometimes by default they won't draw. Have you tried calling View.setWillNotDraw(false) in your constructor?

How do you overline numbers in android xml?

I am developing a maths app in which i have to use numbers with bar on top. check this link http://en.wikipedia.org/wiki/Overline
Also how to use Exponentiation like in the following link http://en.wikipedia.org/wiki/Exponentiation in xml file.
This is not the perfect solution but it will give you the idea for text overline.
public class OverLineTextView extends TextView {
private Paint paint;
public OverLineTextView(Context context) {
super(context);
init();
}
public OverLineTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public OverLineTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.GREEN);
paint.setStyle(Style.STROKE);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float width = getPaint().measureText(getText().toString());
canvas.drawLine(getTotalPaddingLeft(), getTotalPaddingTop() + 1,
getTotalPaddingLeft() + width, getTotalPaddingTop() + 1, paint);
}
}

How to strike through obliquely on TextView

Is there any simple way how to draw obliquely strike through on TextView? Now I'm using this code:
textview.setPaintFlags(textview.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
But I need something like this:
I'm not very familiar with Paint API how to simply achive this so any help will be appreciated. Thank you.
It's pretty easy with a custom TextView class. Check out the Canvas guide from the documentation for more info on this. Note the comments where you can change the color/width:
public class ObliqueStrikeTextView extends TextView
{
private int dividerColor;
private Paint paint;
public ObliqueStrikeTextView(Context context)
{
super(context);
init(context);
}
public ObliqueStrikeTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context);
}
public ObliqueStrikeTextView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init(context);
}
private void init(Context context)
{
Resources resources = context.getResources();
//replace with your color
dividerColor = resources.getColor(R.color.black);
paint = new Paint();
paint.setColor(dividerColor);
//replace with your desired width
paint.setStrokeWidth(resources.getDimension(R.dimen.vertical_divider_width));
}
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawLine(0, getHeight(), getWidth(), 0, paint);
}
}
You can use it in a layout file by fully qualifying the view with your package, like this:
<your.package.name.ObliqueStrikeTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1234567890"
android:textSize="20sp"/>
Here's the end result:

Adding a custom rectangle to a custom RelativeLayout

I have a custom RelativeLayout, where i want to dynamically create Rectangles.
They arent shown in my current code, but i dont know whats the reason.
Custom RelativeLayout:
public class DrawView extends RelativeLayout {
public DrawView(Context context) {
super(context);
setFocusable(true);
this.addView(new Rectangle(this.getContext()));
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
Custom Rectangle:
public class Rectangle extends View {
public Rectangle(Context context) {
super(context);
}
public Rectangle(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Rectangle(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawRect(new Rect(), paint);
}
}
EDIT:
Solution was:
public class Rectangle extends View {
public Rectangle(Context context) {
super(context);
init();
}
public Rectangle(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public Rectangle(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
this.setBackgroundResource(R.drawable.rectangle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = 150;
int height = 50;
setMeasuredDimension(width, height);
}
}
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="5sp"/>
<gradient
android:angle="315"
android:startColor="#FFFF6666"
android:endColor="#FFFF1111"
android:type="linear"
/>
</shape>
I think you are missing to set your Shape's LayoutParams. Try tho create a Rectangle object, set its width + height and then add it to your RelativeLayout :D

Categories

Resources