I have created my own view. It is a semicircle. I want it to align it to the bottom of the screen. Since, I drew it using drawArc, it does not align to the bottom. I have tried the alternative of setting a circle with center's y co-ordinate co-incident with screen bottom co-ordinate. I do not wish to use it as I wish to implement some additional things to it. Here is my code for custom view.
package legacy_systems.customview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
public class CircleView extends View{
private Paint markerPaint;
private Paint textPaint;
private Paint circlePaint;
private int textHeight;
public CircleView(Context context)
{
super(context);
initCircleView();
}
public CircleView(Context context, AttributeSet attrs)
{
super(context, attrs);
initCircleView();
}
public CircleView(Context context, AttributeSet attrs, int defaultStyle)
{
super(context, attrs, defaultStyle);
initCircleView();
}
protected void initCircleView()
{
setFocusable(true);
circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
circlePaint.setColor(getResources().getColor(android.R.color.black));
circlePaint.setStrokeWidth(3);
circlePaint.setStyle(Paint.Style.STROKE);
markerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
markerPaint.setColor(getResources().getColor(android.R.color.darker_gray));
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int measuredWidth = measure(widthMeasureSpec);
int measuredHeight = measure(heightMeasureSpec);
int d = Math.min(measuredWidth, measuredHeight);
setMeasuredDimension(d,d);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
int px, py;
px = getMeasuredWidth()/2;
py = getMeasuredHeight()/2;
int radius = Math.min(px, py);
RectF oval = new RectF();
oval.set(px-radius, py-radius, px+radius, py+radius);
canvas.drawArc(oval,180,180,false, circlePaint);
//canvas.drawCircle(px, py, radius, circlePaint);
canvas.save();
}
private int measure(int measureSpec)
{
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if(specMode == MeasureSpec.UNSPECIFIED)
{
result = 200;
}
else
{
result = specSize;
}
return result;
}
}
I want to align the semi-circle to bottom of screen. Any ideas how I can do it ?
First of all, thanks all of you for your answers. Your answer is partially correct. Yes, with
android:layout_alignParentBottom="true"
would do the half trick. Suppose I have a circle to draw. In custom view, (0,0) represents the heightest leftmost co-ordinate of the view, not of the screen. And suppose one wants to half circle to be located at thebottom of screen just as I wanted to do, co-ordinate guessing turns out to be a problem. Here is how I actually solved it.
px = getMeasuredWidth()/2;
py = getMeasuredHeight();
canvas.drawCircle(px, py, radius, paint);
Here, px
would be the centre of whole width of view, midpoint of view width and py
would be the last screen y axis co-ordinate assigned to view.
Hope it helps somebody else too.
You can try to use the:
android:gravity="bottom"
or
android:layout_gravity="bottom"
property.
Make sure that the screen on which you want to put/add it is a RelativeLayout
.
Do this in the layout
<legacy_systems.customview.CircleView
android:id="@+id/myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"/>
In your onCreate() method
CircleView view = (CircleView) findViewById(R.id.myview);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.