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
Related
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
}
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?
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);
}
}
I am trying to make custom Views in android I mention all my arrtrs list in MainActivity and the XML file but I there is an error in main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews.piechart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.customviews.charting.piechart.MainActivity
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
custom:titleText="Background color"
custom:valueColor="#android:color/holo_green_light"
/>
</LinearLayout>
Do you have a constructor with no arguments?
You should add a constructor like:
public MainActivity(Context context, AttributeSet attrs) {
super(context, attrs);
}
or
public MainActivity(Context context) {
super(context);
}
You asked for a simple example of a custom view:
public class BirdView extends ImageView {
private float direction = 0;
Paint paint = new Paint();
public BirdView(Context context) {
super(context);
}
public BirdView(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(6);
}
public BirdView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
MeasureSpec.getSize(heightMeasureSpec));
}
#Override
public void draw(Canvas canvas) {
canvas.save();
int stratX = getWidth() / 2;
int startY = getHeight() / 2;
canvas.rotate(direction, stratX, startY);
super.draw(canvas);
canvas.restore();
canvas.drawLine(stratX, startY, stratX, startY + 80, paint);
}
public void update(float dir) {
direction = dir;
invalidate();
}
}
I am trying to make a simple custom view which is not working. I refereed the custom view class from layout resource file. But its not working. Can anybody tell me where the problem is.
public class WriteOnScreenActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
public class CustomView extends View {
private Paint paint;
public TouchEventView(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.RED);
paint.setTextSize(25);
paint.setAntiAlias(true);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText("Hello World", 5, 30, paint);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.touch.CustomView
android:id="#+id/TouchEventView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
Move the definition of your class TouchEventView into a new file.
And here's the fix.
public class TouchEventView extends View {
private Paint paint;
public TouchEventView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setup(context, attrs, defStyle);
}
public TouchEventView(Context context, AttributeSet attrs) {
super(context, attrs);
setup(context, attrs, 0);
}
public TouchEventView(Context context) {
super(context);
setup(context, null, 0);
}
private void setup(Context context, AttributeSet attrs, int defStyle)
{
paint = new Paint();
paint.setColor(Color.RED);
paint.setTextSize(25);
paint.setAntiAlias(true);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText("Hello World", 5, 30, paint);
}
}