Animate a View in Fragment - android

I have created a custom semi-donutchart(dash chart) for my Android app (I could not find any library to do it..If you know any comment it).
EDIT: I tried animate its entry (the details are given below) and it did not work. So I started searching for how to animate a normal TextView or ImageView in a fragment and I was unable to do so. Now I am changing the post so people can learn how to animate a view (Not just a custom view) in a fragment. Any answers that say how to animate a view in a fragment are welcome.
The code is as follows
public class DonutChart extends View {
private float radius;
Paint paint;
Paint shadowPaint;
int a,b,c;
Path myPath;
Path shadowPath;
RectF outterCircle;
RectF innerCircle;
RectF shadowRectF;
public DonutChart(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,R.styleable.DonutChart,0, 0);
try {
radius = a.getDimension(R.styleable.DonutChart_radius, 20.0f);
} finally {
a.recycle();
}
paint = new Paint();
paint.setDither(true);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setAntiAlias(true);
paint.setStrokeWidth(radius / 14.0f);
shadowPaint = new Paint();
shadowPaint.setColor(0xf0000000);
shadowPaint.setStyle(Paint.Style.STROKE);
shadowPaint.setAntiAlias(true);
shadowPaint.setStrokeWidth(6.0f);
shadowPaint.setMaskFilter(new BlurMaskFilter(4, BlurMaskFilter.Blur.SOLID));
myPath = new Path();
shadowPath = new Path();
outterCircle = new RectF();
innerCircle = new RectF();
shadowRectF = new RectF();
float adjust = (.019f*radius);
shadowRectF.set(adjust, adjust, radius*2-adjust, radius*2-adjust);
adjust = .038f * radius;
outterCircle.set(adjust, adjust, radius*2-adjust, radius*2-adjust);
adjust = .276f * radius;
innerCircle.set(adjust, adjust, radius * 2 - adjust, radius * 2 - adjust);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// draw shadow
paint.setShader(null);
float adjust = (.0095f*radius);
paint.setShadowLayer(8, adjust, -adjust, 0xaa000000);
drawDonut(canvas, paint, 180, (180+a+b+c));
//Maroon
//setGradient(0xff84BC3D,0xff5B8829);
setGradient(0xffffb300,0xffffb300);
drawDonut(canvas,paint, (180+a),b);
//Violet
//setGradient(0xffe04a2f,0xffB7161B);
setGradient(0xffd32f2f,0xfff44336);
drawDonut(canvas, paint, 180,a);
// White
// setGradient(Color.TRANSPARENT,Color.TRANSPARENT);
//drawDonut(canvas, paint, 0, 180);
// Green
setGradient(0xff388e3c,0xff66bb6a);
drawDonut(canvas, paint,(180+a+b),c);
}
public void drawDonut(Canvas canvas, Paint paint, float start,float sweep){
myPath.reset();
myPath.arcTo(outterCircle, start, sweep, false);
myPath.arcTo(innerCircle, start+sweep, -sweep, false);
myPath.close();
canvas.drawPath(myPath, paint);
}
public void setGradient(int sColor, int eColor){
paint.setShader(new RadialGradient(radius, radius, radius - 5,
new int[]{sColor, eColor},
new float[]{.6f, .95f}, TileMode.CLAMP));
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int desiredWidth = (int) radius*2;
int desiredHeight = (int) radius*2;
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width;
int height;
//70dp exact
if (widthMode == MeasureSpec.EXACTLY) {
width = widthSize;
}else if (widthMode == MeasureSpec.AT_MOST) {
//wrap content
width = Math.min(desiredWidth, widthSize);
} else {
width = desiredWidth;
}
//Measure Height
if (heightMode == MeasureSpec.EXACTLY) {
height = heightSize;
} else if (heightMode == MeasureSpec.AT_MOST) {
height = Math.min(desiredHeight, heightSize);
} else {
height = desiredHeight;
}
//MUST CALL THIS
setMeasuredDimension(width, height);
}
public void getData(int x,int y){
invalidate();
a=((x*180)/10);
b=(y*180)/10;
c=180-(a+b);
a=90;
b=40;
c=(180-(a+b));
//Toast.makeText(getContext(),"Inside Chart "+s1+" "+s2+" "+s3 +" "+String.valueOf(x),Toast.LENGTH_SHORT).show();
}
}
The layout for the graph is defined as follows
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:customviews="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#android:color/transparent"
android:id="#+id/statistics_1_page"
tools:context=".MainActivity">
<com.spintum.preexam.DonutChart
android:id="#+id/donutChart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
customviews:radius="100dp"
android:gravity="center"
android:layout_gravity="center"
android:layout_marginTop="75dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/PerformanceRating"
android:textSize="22sp"
android:textStyle="bold"
android:gravity="center"
android:text="You Average Score is 80%"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/PerformanceRating_1"
android:textSize="18sp"
android:textStyle="bold|italic"
android:layout_gravity="center"
android:text="Don't Stop till you reach 100"/>
</LinearLayout>
I inflate the layout using a fragment as follows
public class Fragment_Statistics extends Fragment {
DonutChart chart;
public View onCreateView(LayoutInflater layoutInflater,ViewGroup container,Bundle SavedInstances){
View statistics_fragment=getActivity().getLayoutInflater().inflate(R.layout.fragment_statistics, null);
chart = (DonutChart) statistics_fragment.findViewById(R.id.donutChart);
chart.getData(2, 2);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Delay and give the device enough time to render the chart
}
}, 2000);
Animation animation_clock=AnimationUtils.loadAnimation(getActivity(),R.anim.clockwise);
chart.startAnimation(animation_clock);
return statistics_fragment;
}
#Override
public void onResume() {
super.onResume();
chart.invalidate();
}
ClockWise animation defined in the fragment is as follows
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000" >
</rotate>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="5000"
android:fromDegrees="360"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000" >
</rotate>
I think I have done everything correctly but no animation is created and the page just appears with the chart..
Can someone tell me what is wrong with my code or is there a better way to do it than the one I have tried to do?

Related

Android oval shape 2 colors

I have a linear layout view whose background I have set to oval shape solid color. So far I have the background as circle. Now I want to achieve same i.e using shape drawable to get a circle with 2 colors. Please see attached.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
android:centerX="-1"
android:type="sweep"
android:startColor="color1"
android:endColor="color2"
/>
</shape>
create shape.xml in your drawable folder
shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<gradient android:startColor="#0000FF" android:endColor="#00FF00"
android:angle="270"/>
</shape>
This might come late, but I had trouble finding good answer so hear my take.
I used a custom drawable to draw the circle and with it a LinearGradient shader which is configured by positions array to have no gradient transition. The gradient line direction is configured in LinearGradient constructor (here it is diagonal).
public class MultiColorCircleDrawable extends Drawable {
private Paint paint;
private int[] colors;
public MultiColorOvalDrawable(int[] colors) {
this.colors = colors;
}
private void init() {
paint = new Paint();
paint.setShader(createShader());
}
private Shader createShader() {
int[] colorsArray = new int[colors.length * 2];
float[] positions = new float[colors.length * 2];
for (int i = 0; i < colors.length; i++) {
int y = i * 2;
int color = colors[i];
colorsArray[y] = color;
colorsArray[y+1] = color;
positions[y] = 1f / colors.length * i;
positions[y+1] = 1f / colors.length * (i+1);
}
Rect bounds = getBounds();
int width = bounds.right - bounds.left;
int height = bounds.bottom - bounds.top;
return new LinearGradient(0, 0, width, height, colorsArray, positions, Shader.TileMode.REPEAT);
}
#Override
public void draw(Canvas canvas) {
if (null == paint) {
init();
}
Rect bounds = getBounds();
int width = bounds.right - bounds.left;
int height = bounds.bottom - bounds.top;
canvas.drawCircle(width/2, height/2, (width/2) - strokeWidth, maskPaint);
}
#Override
public void setAlpha(int i) {
}
#Override
public void setColorFilter(ColorFilter colorFilter) {
}
#Override
public int getOpacity() {
return PixelFormat.OPAQUE;
}
}

How to create layout with buttons like on this picture? ( BUTTON THAT HAVE ACTUALLY SHAPE OF TRIANGLE, NOT MASK )

This is wanted layout:
Layout
I'm stuck with this for a long time. Trying to rotate somekind buttons to mask other one and other ways like with:
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="87%" />
How to write code for this layout, buttons need to be custom shaped, not to be able with clicking around them, to achieve clicking same?
EDIT I tried Making a triangle shape using xml definitions? , Creating a triangular shaped button for android application
but this what i got: http://i.stack.imgur.com/X88P0.png
Here is the code of TriangleButton which draw triangle and react on clickEvent() only if your finger position in triangle zone. Here you can set colors(normal and pressed):
Class:
public class TriangleButton extends Button {
private final float RADIUS = 50.0f;
private boolean mIsPressed;
private Region mPathRegion;
private final Path mPath = new Path();
private final Paint mNormalPaint = new Paint(Paint.ANTI_ALIAS_FLAG) {
{
setDither(true);
setStyle(Style.FILL);
setPathEffect(new CornerPathEffect(RADIUS));
}
};
private final Paint mPressedPaint = new Paint(Paint.ANTI_ALIAS_FLAG) {
{
setDither(true);
setStyle(Style.FILL);
setPathEffect(new CornerPathEffect(RADIUS));
}
};
public TriangleButton(final Context context) {
this(context, null);
}
public TriangleButton(final Context context, final AttributeSet attrs) {
this(context, attrs, 0);
}
public TriangleButton(final Context context, final AttributeSet attrs, final int defStyleAttr) {
super(context, attrs, defStyleAttr);
setWillNotDraw(false);
setBackgroundColor(Color.TRANSPARENT);
setColors(Color.RED, Color.GRAY);
}
public void setColors(final int color, final int pressed) {
mNormalPaint.setColor(color);
mPressedPaint.setColor(pressed);
}
#Override
protected void onSizeChanged(final int w, final int h, final int oldw, final int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// Set triangle
mPath.reset();
mPath.moveTo(0.0f, h);
mPath.lineTo(w / 2.0f, 0.0f);
mPath.lineTo(w, h);
mPath.close();
// Create region for touch detecting
final RectF rectF = new RectF();
mPath.computeBounds(rectF, true);
mPathRegion = new Region();
mPathRegion.setPath(
mPath,
new Region(
(int) rectF.left,
(int) rectF.top,
(int) rectF.right,
(int) rectF.bottom)
);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (mPathRegion.contains((int) event.getX(), (int) event.getY())) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mIsPressed = true;
break;
case MotionEvent.ACTION_UP:
mIsPressed = false;
performClick();
break;
case MotionEvent.ACTION_CANCEL:
mIsPressed = false;
break;
}
postInvalidate();
} else {
mIsPressed = false;
postInvalidate();
return false;
}
postInvalidate();
return true;
}
#Override
protected void onDraw(final Canvas canvas) {
canvas.drawPath(mPath, mIsPressed ? mPressedPaint : mNormalPaint);
super.onDraw(canvas);
}
}
XML:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.user.timezonetest.TriangleButton
android:id="#+id/btn_bottom"
android:layout_width="300dp"
android:layout_height="250dp"
android:text="Second Button"
android:textColor="#000"
android:layout_gravity="center_horizontal"
android:gravity="bottom|center_horizontal"
android:padding="30dp"/>
<com.example.user.timezonetest.TriangleButton
android:id="#+id/btn_top"
android:layout_width="180dp"
android:layout_height="160dp"
android:text="First Button"
android:textColor="#000"
android:layout_gravity="center_horizontal"
android:gravity="bottom|center_horizontal"
android:layout_margin="30dp"
android:padding="30dp"/>
</FrameLayout>
Code:
final TriangleButton bottomButton = (TriangleButton) findViewById(R.id.btn_bottom);
final TriangleButton topButton = (TriangleButton) findViewById(R.id.btn_top);
bottomButton.setColors(Color.RED, Color.GRAY);
topButton.setColors(Color.YELLOW, Color.GRAY);
You can define two Button with link you provide and then put both of them in same FrameLayout with something like following code :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="300dp" >
<Button
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#drawable/rectangle_1" />
<Button
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#drawable/rectangle_2" />
/>
Create rectangle_1 and rectangle_2 with links in comment.

I want to place imageview in circular motion in xml

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.24">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/background_tree"
/>
<AbsoluteLayout
android:id="#+id/llMapContainer"
android:layout_width="match_parent"
android:layout_height="390dp"
android:layout_x="0dp"
android:layout_y="44dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/imgLocation"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_x="48px"
android:layout_y="440px"
android:src="#drawable/character_icon" />
</AbsoluteLayout>
</RelativeLayout>
</LinearLayout>
I want to place ImageView in the circles of the tree in my xml file . I tried using coordinates but it changes its positions with different screen size .
Step 1:Create CircularimageView.class
public class CircularImageView extends ImageView {
private int borderWidth;
private int viewWidth;
private int viewHeight;
private Bitmap image;
private Paint paint;
private Paint paintBorder;
private BitmapShader shader;
public CircularImageView(Context context) {
super(context);
setup();
}
public CircularImageView(Context context, AttributeSet attrs) {
super(context, attrs);
setup();
}
public CircularImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setup();
}
private void setup() {
// init paint
paint = new Paint();
paint.setAntiAlias(true);
paintBorder = new Paint();
setBorderColor(Color.WHITE);
paintBorder.setAntiAlias(true);
this.setLayerType(LAYER_TYPE_SOFTWARE, paintBorder);
this.setBorderWidth(2);
paintBorder.setShadowLayer(4.0f, 0.0f, 2.0f, Color.BLACK);
}
public void setBorderWidth(int borderWidth) {
this.borderWidth = borderWidth;
this.invalidate();
}
public void setBorderColor(int borderColor) {
if (paintBorder != null)
paintBorder.setColor(borderColor);
this.invalidate();
}
private void loadBitmap() {
BitmapDrawable bitmapDrawable = (BitmapDrawable) this.getDrawable();
if (bitmapDrawable != null)
image = bitmapDrawable.getBitmap();
}
#SuppressLint("DrawAllocation")
#Override
public void onDraw(Canvas canvas) {
// load the bitmap
loadBitmap();
// init shader
if (image != null) {
shader = new BitmapShader(Bitmap.createScaledBitmap(image, canvas.getWidth(), canvas.getHeight(), false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint.setShader(shader);
int circleCenter = viewWidth / 2;
// circleCenter is the x or y of the view's center
// radius is the radius in pixels of the cirle to be drawn
// paint contains the shader that will texture the shape
canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter + borderWidth - 4.0f, paintBorder);
canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter - 4.0f, paint);
}
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = measureWidth(widthMeasureSpec);
int height = measureHeight(heightMeasureSpec, widthMeasureSpec);
viewWidth = width - (borderWidth * 2);
viewHeight = height - (borderWidth * 2);
setMeasuredDimension(width, height);
}
private int measureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else {
// Measure the text
result = viewWidth;
}
return result;
}
private int measureHeight(int measureSpecHeight, int measureSpecWidth) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpecHeight);
int specSize = MeasureSpec.getSize(measureSpecHeight);
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else {
// Measure the text (beware: ascent is a negative number)
result = viewHeight;
}
return (result + 2);
}
}
Step2:In layout.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.24">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.packagename.CircularImageView
android:id="#+id/imgLocation"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_x="48px"
android:layout_y="440px"
android:src="#drawable/character_icon" />
</RelativeLayout>
</LinearLayout>

Canvas in HorizontalScrollView

I faced with such a problem that Canvas does not paint in HorizontalScrollView. Instead, I get a blank screen. But if we add any other object, then everything is working perfectly.I have someone with that?
Here my Point.class
public class Point extends View implements Serializable {
private float x;
private float y;
private Paint paint = new Paint();
public Point(Context context) {
super(context);
paint.setColor(Color.BLUE);
paint.setStrokeWidth(5);
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
#Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawCircle(getX(), getY(), 5f, paint);
}
}
My Field class, where I want to display my points
public class GraphicField extends RelativeLayout {
private RelativeLayout scrollLayout;
public GraphicField(Context context) {
super(context);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.graph_field, this);
scrollLayout = (RelativeLayout) view.findViewById(R.id.scrollLayout);
init();
}
private void init() {
Point point = new Point(getContext());
point.setX(10);
point.setY(10);
scrollLayout.addView(point);
point = new Point(getContext());
point.setX(200);
point.setY(200);
scrollLayout.addView(point);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<LinearLayout
android:id="#+id/leftLayout"
android:layout_width="10dp"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:gravity="right"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp">
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#color/blue_dark" />
</LinearLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#+id/leftLayout"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/scrollLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:background="#color/blue_dark"
android:minWidth="1000dp" />
</RelativeLayout>
</HorizontalScrollView>
</RelativeLayout>
If I replace HorizontalScrollView to RelativeLayout, then points perfectly displaying. Please help someone
What seems to be wrong is that your view collpases when placed in ScrollViews.
You'll solve the issue by adding the following attributes to your view's xml line codes :
android:minHeight="1000"
android:minWidth="1000"
But best solution is to override the 'onMeasure() method of your CustomView class :
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int desiredWidth = 2000; //enter your width in px
int desiredHeight = 1000; //enter your height in px
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width;
int height;
if (widthMode == MeasureSpec.EXACTLY) {
width = widthSize;
} else if (widthMode == MeasureSpec.AT_MOST) {
width = Math.min(desiredWidth, widthSize);
} else {
width = desiredWidth;
}
if (heightMode == MeasureSpec.EXACTLY) {
height = heightSize;
} else if (heightMode == MeasureSpec.AT_MOST) {
height = Math.min(desiredHeight, heightSize);
} else {
height = desiredHeight;
}
setMeasuredDimension(width, height);
}

Size of Remaining View to Draw Custom View

I have to draw a custom view below A banner for that like the pick attached PICK
below DaTTab banner I have to draw the Rectangle view my xml
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include android:id="#+id/banner" layout="#layout/upper_border" />
<com.example.ui_1.Border_Portrait
android:id="#+id/custom_display_view1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#+id/banner"
/>
Draw_Border.jav
class Border_Portrait extends View{
private static final int DEFAULT_SIZE = 100;
int mViewHeight , mViewWidth ;
Paint mPaint ;
Canvas mCanvas ;
int margin ;
float [] mPoints ;
int mLogoHeight ;
int mLogoWidth ;
int dy,dx ;
public Border_Portrait(Context context) {
super(context);
}
public Border_Portrait(Context context, AttributeSet attrs) {
super(context, attrs);
margin = 7 ;
mPaint = new Paint();
mPaint.setColor(Color.rgb(0,188,226));
mPaint.setStrokeWidth(3);
mLogoHeight = ScreenUtils.convertDIPToPixels(getContext(), 50 + margin );
mLogoWidth = ScreenUtils.convertDIPToPixels(getContext(), 66 + margin);
dy = ScreenUtils.convertDIPToPixels(getContext(),1) ;
dx= ScreenUtils.convertDIPToPixels(getContext(),1) ;
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.save();
mPoints = new float [] {
margin + mLogoWidth ,margin,//1
getWidth()-margin ,margin ,//1
getWidth()-margin ,margin ,//2
getWidth()-margin , getHeight()-margin ,//2
getWidth()-margin , getHeight()-margin , //3
margin , getHeight()-margin , //3
margin , getHeight() - margin , //4
margin , mLogoHeight + dy ,//4
margin , mLogoHeight + dy , // 5
margin + mLogoWidth + dx ,mLogoHeight + dy, //5
margin + mLogoWidth ,mLogoHeight + dy, //6
margin + mLogoWidth ,margin, //6
} ;
canvas.drawLines(mPoints, mPaint);
canvas.restore();
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.setMeasuredDimension(parentWidth, parentHeight);
}
private int calculateMeasure(int measureSpec) {
return 0 ;
}
}
my question is how to measure the Width of the border at run time I have refer
THIS
and other links too but till now no luck
Just got the answer add margin in main.xml
<com.example.ui_1.Border_Portrait
android:id="#+id/custom_display_view1"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

Categories

Resources