Sudoku crashes when game starts - android

Hi I am a beginner to android and I am doing a basic sudoku application to study android and finds myself stuck when i start the game..APP CRASH..!!!!
Whenever I try to start the game it crashes and takes me back to the menu screen in my emulator.Someone please help me on this..
Thanks in advance.
The following is my code:
puzzleview.java
package org.example.sudoku;
public class puzzleview extends View {
private static final String TAG="SUDOKU";
private float width;
private float height;
private int selX;
private int selY;
private final Rect selRect= new Rect();
private final Game game;
public puzzleview(Context context){
super(context);
this.game=(Game) context;
setFocusable(true);
setFocusableInTouchMode(true);
}
#Override
protected void onSizeChanged(int w,int h,int oldh,int oldw){
width = w/9f;
height = h/9f;
getRect(selX, selY, selRect);
Log.d(TAG, "onSizeChanged width " + width +", height "+ height);
super.onSizeChanged(w, h, oldw, oldh);
}
private void getRect(int selX2, int selY2, Rect selRect2) {
// TODO Auto-generated method stub
}
#Override
protected void onDraw(Canvas canvas){
Paint background=new Paint();
background.setColor(getResources().getColor(R.color.puzzle_background));
canvas.drawRect(0,0, getWidth(), getHeight(), background);
Paint dark = new Paint();
dark.setColor(getResources().getColor(R.color.puzzle_dark));
Paint hilite = new Paint();
hilite.setColor(getResources().getColor(R.color.puzzle_hilite));
Paint light = new Paint();
light.setColor(getResources().getColor(R.color.puzzle_light));
for (int i = 0; i < 9; i++) {
canvas.drawLine(0, i * height, getWidth(), i * height,
light);
canvas.drawLine(0, i * height + 1, getWidth(), i * height
+ 1, hilite);
canvas.drawLine(i * width, 0, i * width, getHeight(),
light);
canvas.drawLine(i * width + 1, 0, i * width + 1,
getHeight(), hilite);
}
for(int i=0; i<9; i++){
if(i%3!=0)
continue;
canvas.drawLine(0,i*height,getWidth(),i*height,dark);
canvas.drawLine(0, i * height + 1, getWidth(), i * height
+ 1, hilite);
canvas.drawLine(i * width, 0, i * width, getHeight(), dark);
canvas.drawLine(i * width + 1, 0, i * width + 1,
getHeight(), hilite);
}
Paint foreground = new Paint(Paint.ANTI_ALIAS_FLAG);
foreground.setColor(getResources().getColor(R.color.puzzle_foreground));
foreground.setStyle(Style.FILL);
foreground.setTextSize (height * 0.75f);
foreground.setTextScaleX(width/height);
foreground.setTextAlign(Paint.Align.CENTER);
FontMetrics fm=foreground.getFontMetrics();
float x=width/2;
float y=height/2 - (fm.ascent+fm.descent)/2;
for(int i=0;i<9;i++){
for(int j = 0;j<9;j++){
Line 95 ---->: canvas.drawText(this.game.getTileString(i, j), i
* width + x, j * height + y, foreground);
}
}
}
}
logcat:
06-09 10:51:30.163: E/AndroidRuntime(2266): at org.example.sudoku.puzzleview.onDraw(puzzleview.java:95)

It depends how you are using your view, but if you are using it in XML then you need to add another constructor
public puzzleview(Context context, AttributeSet attrs){
super(context, attrs);
this.game=(Game) context;
setFocusable(true);
setFocusableInTouchMode(true);
}

I would imagine that line 95 accesses something which hasn't been initialised. When the app. starts it will quite probably try and render puzzleview. At a guess, game hasn't been fully initialised at this point, and so the access to game.getTileString(i, j) fails in some way. You might like to have a flag in Game which tells puzzleview when getTileString is ready to be used, which you use in puzzleview.onDraw():
if (game.getTileStringsAvailable()) {
for (int i=0;i<9;i++){
for (int j = 0;j<9;j++){
canvas.drawText(this.game.getTileString(i, j), i * width + x, j * height + y, foreground);
}
}
}

Related

How to draw a lines inside a circle angle wise?

In android I have create custom view.I have first drawn circle and now I want to draw a line angle wise within a circle. I want like this with animation.
https://drive.google.com/file/d/1Qx0MBu-77JIlQTByqGTyD-KtGKOB8naG/view?usp=sharing
I have used canvas to draw circle and lines.I have taken viewpager.If I swipe viewpager then pie graphics will rotate.
What I have done uptill now. When animating it's always start from zero:
https://drive.google.com/file/d/12mmAUOeY77jAlj_GmM3Ymcx5m34vli3X/view?usp=sharing
I have done below code:
public class PieView : View
{
int w, h, pl, pr, pt, pb, usableWidth, usableHeight, radius, cx, cy, lineLenght;
Paint paint;
public Canvas canvas;
public float firstLineangle = 0;
public float secondLineangle = 40;
public float thirdLineangle = 120;
float currentAngle,maxAngle;
public override void Draw(Canvas canvas)
{
base.Draw(canvas);
w = Width;
h = Height;
pl = PaddingLeft;
pr = PaddingRight;
pt = PaddingTop;
pb = PaddingBottom;
this.canvas = canvas;
usableWidth = w - (pl + pr);
usableHeight = h - (pt + pb);
radius = Math.Min(usableWidth, usableHeight) / 2;
cx = pl + (usableWidth / 2);
cy = pt + (usableHeight / 2);
lineLenght = radius - (pl * 2) - (pr * 2);
paint = new Paint();
paint.Color = Android.Graphics.Color.White;
paint.SetStyle(Paint.Style.Stroke);
paint.StrokeWidth = 5;
canvas.DrawCircle(cx, cy, radius - 5, paint);
Drawline(canvas, firstLineangle);
Drawline(canvas, secondLineangle);
Drawline(canvas, thirdLineangle);
PostInvalidateDelayed(500);
Invalidate();
}
public void Drawline(Canvas canvas, float angle)
{
float displacedAngle = angle - 90;
float x = cx + ((float)Math.Cos(degreesToRadians(displacedAngle)) * (radius - 5)); //convert angle to radians for x and y coordinates
float y = cy + ((float)Math.Sin(degreesToRadians(displacedAngle)) * (radius - 5));
canvas.DrawLine(cx, cy, x, y, paint); //draw a line from center point back to the point
}
public double degreesToRadians(double degrees)
{
return (degrees * Math.PI) / 180;
}
}
public class PieAnimation : Android.Views.Animations.Animation
{
private PieView pieView;
private float firstLineangle;
private float secondLineangle;
private float thirdLineangle;
public PieAnimation(PieView pieView, float firstLineangle,float secondLineangle,float thirdLineangle)
{
this.pieView = pieView;
this.firstLineangle = firstLineangle;
this.secondLineangle = secondLineangle;
this.thirdLineangle = thirdLineangle;
}
protected override void ApplyTransformation(float interpolatedTime, Transformation t)
{
pieView.firstLineangle = 0 + ((firstLineangle) * interpolatedTime);
pieView.secondLineangle = 0 + ((secondLineangle) * interpolatedTime);
pieView.thirdLineangle = 0 + ((thirdLineangle) * interpolatedTime);
pieView.RequestLayout();
}
}
public class TourPager : Java.Lang.Object, ViewPager.IOnPageChangeListener, ViewPager.IPageTransformer
{
private ViewPager mViewPager;
private float mLastOffset;
public TourView _context;
public TourPager(ViewPager viewpager, TourView context)
{
mViewPager = viewpager;
viewpager.AddOnPageChangeListener(this);
_context = context;
}
public void OnPageSelected(int position)
{
if (position == 0)
{
PieAnimation animation = new PieAnimation(_context._pieView, 0, 40, 120);
animation.Duration = (1000);
_context._pieView.StartAnimation(animation);
}
if (position==1)
{
PieAnimation animation = new PieAnimation(_context._pieView, 100, 140, 200);
animation.Duration=(1000);
_context._pieView.StartAnimation(animation);
}
if(position==2)
{
PieAnimation animation = new PieAnimation(_context._pieView, 180, 270, 10);
animation.Duration = (1000);
_context._pieView.StartAnimation(animation);
}
}
There is running GIF.
There is PieView.cs.
public class PieView:View
{
int w, h, pl, pr, pt, pb, usableWidth, usableHeight, radius, cx, cy, lineLenght;
int handTruncation, hourHandTruncation = 0;
Paint paint;
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
Rect rect = new Rect();
public PieView(Context context) : base(context)
{
}
public PieView(Context context, IAttributeSet attrs) : base(context, attrs)
{
}
public PieView(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
}
public PieView(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
{
}
public override void Draw(Canvas canvas)
{
base.Draw(canvas);
w = Width;
h = Height;
pl = PaddingLeft+10;
pr = PaddingRight+10;
pt = PaddingTop+10;
pb = PaddingBottom+10;
usableWidth = w - (pl + pr);
usableHeight = h - (pt + pb);
radius = Math.Min(usableWidth, usableHeight) / 2;
cx = pl + (usableWidth / 2);
cy = pt + (usableHeight / 2);
int min = Math.Min(usableWidth, usableHeight);
handTruncation = min / 20;
hourHandTruncation = min / 7;
lineLenght = radius - (pl * 2) - (pr * 2);
paint = new Paint();
paint.Color = Android.Graphics.Color.White;
paint.SetStyle(Paint.Style.Stroke);
paint.StrokeWidth = 5;
canvas.DrawCircle(cx, cy, radius , paint);
drawNumeral(canvas);
drawHands(canvas);
PostInvalidateDelayed(200);
Invalidate();
}
private void drawHands(Canvas canvas)
{
Calendar c = Calendar.Instance;
float hour = c.Get(CalendarField.HourOfDay);
hour = hour > 12 ? hour - 12 : hour;
drawHand1(canvas, (hour + c.Get(CalendarField.Minute) / 60) * 5f,true);
drawHand1(canvas, c.Get(CalendarField.Minute),false);
drawHand1(canvas, c.Get(CalendarField.Second),false);
}
private void drawNumeral(Canvas canvas)
{
paint.TextSize=50;
foreach (var number in numbers)
{
string tmp = number.ToString();
paint.GetTextBounds( tmp, 0, tmp.Length, rect); //getTextBounds(tmp, 0, tmp.length(), rect);
double angle = Math.PI / 6 * (number - 3);
int x = (int)(w / 2 + Math.Cos(angle) * radius - rect.Width() / 2);
int y = (int)(h / 2 + Math.Sin(angle) * radius + rect.Height() / 2);
canvas.DrawText(tmp, x, y, paint);
}
}
private void drawHand1(Canvas canvas, double loc, bool isHour)
{
double angle = Math.PI * loc / 30 - Math.PI / 2;
int handRadius = isHour ? radius - handTruncation - hourHandTruncation : radius - handTruncation;
canvas.DrawLine(Width / 2, Height / 2,
(float)(Width / 2 + Math.Cos(angle) * handRadius),
(float)(Height / 2 + Math.Sin(angle) * handRadius),
paint);
}
}
You can use it in MainActivity.cs
RelativeLayout relativeLayout1 = FindViewById<RelativeLayout>
(Resource.Id.relativeLayout1);
relativeLayout1.SetBackgroundColor(Color.Black);
AddContentView(new PieView(this),new ViewGroup.LayoutParams(-1,-1));

how to make a growing line animation on canvas android

I am trying to make an animation of growing line it will look like that line is moving towards the end of canvas width and the start of the line will be constant only end of line should be growing
here is my code of the class which paint line on android PLZ HELP ANY HELP WOULD BE APPREICATED.
package com.example.line;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.view.View;
public class MyPlay extends View{
float startx = 30;
float starty = 60;
float endx=0;
float endy=0;
public MyPlay(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
Paint textPaint = new Paint();
textPaint.setARGB(50, 254, 10, 50);
textPaint.setColor(Color.RED);
textPaint.setTextAlign(Align.CENTER);
textPaint.setTextSize(20);
canvas.drawText("Made by Skyrush", canvas.getWidth()/2, canvas.getHeight()/2, textPaint);
Paint linePaint = new Paint();
linePaint.setColor(Color.BLACK);
linePaint.setStrokeWidth(2);
//canvas.drawLine(startx, starty, endx, endy, linePaint);
if(endx < canvas.getWidth()){
endx = startx+5;
endy = starty-5;
canvas.drawLine(startx, starty, endx, endy, linePaint);
invalidate();
}
invalidate();
}
}
In this webpage there's a way to make an animation of a line growing.
You just have to implement the following methods in a custom view:
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// starting point
x1 = 50;
y1 = 50;
// ending point
x2 = getWidth() / 2 + getWidth() / 4;
y2 = getHeight() / 2 + getHeight() / 4;
Log.d("line xy xy", x1 + " : "+y1+" : "+x2 + " : "+y2);
divideLineIntoEqualParts();
}
// dividing line into 50 equal parts
private void divideLineIntoEqualParts() {
/*
* Courtesy : www.dummies.com
* (x,y) = (x1 + k(x2 - x1),y1 + k(y2 - y1))
* */
listOfPoints.clear();
for (int k = 1; k <= 50; k++) {
listOfPoints.add(new PointF(x1 + ((k * (x2 - x1)) / 50),y1 + (k * (y2 - y1)) / 50));
}
Log.d("listOfPoints : size : ",listOfPoints.size()+"");
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(inte < listOfPoints.size()){
canvas.drawLine(listOfPoints.get(0).x, listOfPoints.get(0).y, listOfPoints.get(inte).x,listOfPoints.get(inte).y, paint);
inte++;
if(inte < listOfPoints.size()){
invalidate();
}
}

Android : canvas.drawBitmap() method not working properly

I already post two question related this (plz refer this
custom piechart 1 and this custom pie chart 2) but not getting answer ,finally i develop my own but getting stuck at some point .
I need to create lines between drawn area as you can see in image .for inner circle i used canvas.drawArc() .for outer circle i used canvas.drawCircle() and for middle lines i used canvas.drawBitmap() method with differ angle .here inner and outer circle drawn properly but for middle arc ,only first two bitmap/arcs are drawn properly but remaining two are not in their exact position .
I used same code for drawing all bitmap/arcs but not getting exact output .I am getting stuck what is the exact problem.need help ,thanks in advance .
you can see my output
here..
Hear is my onCreate() , in which i generate and set view .
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv1 = (LinearLayout) findViewById(R.id.linear);
matrix = new Matrix();
MyView myview = new MyView(this);
lv1.addView(myview);
}
this is my customview class that i want to generate.
public class MyView extends View {
private Paint p ,paint,paint_text;
private int startX;
private int startY;
private int radius;
private ArrayList<Integer> colors;
private ArrayList<Float> values;
Bitmap bitmap;
Context mContext;
RectF rectF , rectF2 ;
public MyView(Context context) {
super(context);
mContext = context;
p = new Paint();
p.setAntiAlias(true);
colors = new ArrayList<Integer>();
values = new ArrayList<Float>();
Display display = getWindowManager().getDefaultDisplay();
width1 = display.getWidth();
height1 = display.getHeight();
startX = 0 ;
startY = 0;
radius = (int) (width1/2);
colors.add(Color.RED);
colors.add(Color.BLUE);
colors.add(Color.YELLOW);
colors.add(Color.GREEN);
values.add(8f);
values.add(2f);
values.add(4f);
values.add(2f);
}
This is my onDraw() method .
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float _angle1 = 0 ,_angle2 = 0 ,_angle3 = 0 , _angle4 =0;
Bitmap myBitmap1 = BitmapFactory.decodeResource(getResources(),R.drawable.saperate_line);
bitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
float offset = 0;
float angle = (float) 5.60 ;
matrix.reset();
canvas.translate(0,canvas.getHeight()); //reset where 0,0 is located
canvas.scale(1,-1); // for scaling
rectF = new RectF();
rectF.set(-(getStartX() + getRadius()), -(getStartY() + getRadius()), getStartX() + getRadius(),getStartY() + getRadius());
//for creating outer circle using canvas.drawCircle-----------
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(2);
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(0, 0, (float) (width1/(1.4)), paint);
//for creating inner circle using canvas.drawArc-----------
for (int i = 0; i < values.size(); i++) {
p.setColor(colors.get(i));
if (i == 0) {
canvas.drawArc(rectF, offset, values.get(i) * angle, true, p);
_angle1 = ((offset+(values.get(i) * angle)))/2;
matrix.postRotate(_angle1);
canvas.drawBitmap(myBitmap1, matrix, null);
}
if(i == 1){
canvas.drawArc(rectF, offset, values.get(i) * angle, true, p);
_angle2 = ((offset + (values.get(i) * angle)))/2;
matrix.postRotate(_angle2);
canvas.drawBitmap(myBitmap1, matrix, null);
}
if(i == 2){
canvas.drawArc(rectF, offset, values.get(i) * angle, true, p);
_angle3 = ((offset + (values.get(i) * angle)))/2;
// _angle3 = (offset + angle);
matrix.postRotate(_angle3);
canvas.drawBitmap(myBitmap1, matrix, null);
}
if (i == 3){
canvas.drawArc(rectF, offset, values.get(i) * angle, true, p);
_angle4 = ((offset + (values.get(i) * angle)))/2;
matrix.postRotate(_angle4);
canvas.drawBitmap(myBitmap1, matrix, null);
}
offset += (values.get(i) * angle);
Log.e("new offset :: ","****************************"+offset);
}
canvas.save();
}
Thanks to every one.
finally i did it using replacing this stuff Rotating Image on A canvas
matrix.postRotate(_angle1);
canvas.drawBitmap(myBitmap1, matrix, null);
with this
Matrix matrix1 = new Matrix();
matrix1.setRotate(_angle1, 0, 0);
canvas.drawBitmap(myBitmap1, matrix1, null);
here ,first you should change the value like this ,
float myAngle = 0;
myAngle = ((90 * values.get(i)) / 16);

How keep previous draw on same canvas?

I am able to draw a text on canvas on motion view now the problem is that when i draw text & go for the next draw on same canvas my draw text is getting disappear i mean screen is getting redraw because of invalidate i want keep my previous draw and make new draw on same canvas how am i going to do that ?
public class PuzzleView extends View {
private float width; // width of one tile
private float height; // height of one tile
private int selX; // X index of selection
private int selY; // Y index of selection
private final Rect selRect = new Rect();
private final Game game;
float positionX = 5;
float positionY = 15;
String strgettile = null;
float x, y;
Paint foreground = new Paint(Paint.ANTI_ALIAS_FLAG);
String getorientation;
public PuzzleView(Context context) {
super(context);
this.game = (Game) context;
setFocusable(true);
setFocusableInTouchMode(true);
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
width = w / 9f;
height = h / 9f;
// getRect(selX, selY, selRect);
Log.d(TAG, "onSizeChanged: width " + width + ", height " + height);
super.onSizeChanged(w, h, oldw, oldh);
}
#Override
protected void onDraw(Canvas canvas) {
//super.onDraw(canvas);
// canvas.save();
// Draw the background...
Paint background = new Paint();
background.setColor(getResources().getColor(R.color.puzzle_background));
canvas.drawRect(0, 0, getWidth(), getHeight(), background);
// Draw the board...
// Define colors for the grid lines
Paint dark = new Paint();
dark.setColor(getResources().getColor(R.color.puzzle_dark));
Paint hilite = new Paint();
hilite.setColor(getResources().getColor(R.color.puzzle_hilite));
Paint light = new Paint();
light.setColor(getResources().getColor(R.color.puzzle_light));
// Draw the minor grid lines
for (int i = 0; i < 9; i++) {
canvas.drawLine(0, i * height, getWidth(), i * height, light);
canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1,
hilite);
canvas.drawLine(i * width, 0, i * width, getHeight(), light);
canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(),
hilite);
}
// Draw the major grid lines
for (int i = 0; i < 9; i++) {
if (i % 3 != 0)
continue;
canvas.drawLine(0, i * height, getWidth(), i * height, dark);
canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1,
hilite);
canvas.drawLine(i * width, 0, i * width, getHeight(), dark);
canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(),
hilite);
}
// // Draw the numbers...
Paint hint = new Paint();
int m;
if (strgettile != null) {
for (m = 0; m < strgettile.length(); m++) {
System.out.println(strgettile.charAt(m));
char convertst = strgettile.charAt(m);
String characterToString = Character.toString(convertst);
if (getorientation.equalsIgnoreCase("Horizontal")) {
canvas.drawText(characterToString, m * width + positionX,
positionY, foreground); // for motion event
hint.setColor(Color.BLACK);
hint.setTextSize(45);
} else {
canvas.drawText(characterToString, positionX, m * height
+ positionY, foreground);
hint.setColor(Color.BLACK);
hint.setTextSize(45);
}
}
//invalidate();
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() != MotionEvent.ACTION_DOWN)
return super.onTouchEvent(event);
// select((int) (event.getX() / width), (int) (event.getY() /
// height));
game.showKeypadOrError(selX, selY);
foreground.setColor(getResources().getColor(R.color.puzzle_foreground));
foreground.setStyle(Style.FILL);
foreground.setTextSize(height * 0.75f);
foreground.setTextScaleX(width / height);
foreground.setTextAlign(Paint.Align.CENTER);
// // Draw the number in the center of the tile
FontMetrics fm = foreground.getFontMetrics();
// // Centering in X: use alignment (and X at midpoint)
// positionX = width / 2;
// // Centering in Y: measure ascent/descent first
// positionY = height / 2 - (fm.ascent + fm.descent) / 2;
positionX = (int) event.getX();
positionY = (int) event.getY() - (fm.ascent + fm.descent) / 2;
// Draw the numbers...
// Define color and style for numbers
// invalidate();
Log.d(TAG, "onTouchEvent: x " + selX + ", y " + selY);
return true;
}
public void setSelectedTile(String tile, String strorientations) {
// TODO Auto-generated method stub
Log.v("getting string in puzzle view ", tile);
strgettile = tile;
getorientation = strorientations;
invalidate();
}
}
Call invalidate() at the end of onDraw.
This function let the onDraw to get called again.

How to draw a pie chart using RectF which will be easy to support multiple screen size in android?

Hi I am using following code to draw a pie chart in android :-
public class Chart extends View
{
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private float[] value_degree;
private String[] Title_value;
RectF rectf = new RectF(50, 5, 200, 150);
Rect rect = new Rect(50, 5, 200, 150);
float temp = 0;
public Chart(Context context, float[] values, String[] heading)
{
super(context);
value_degree = new float[values.length];
Title_value = new String[heading.length];
for (int i = 0; i < values.length; i++)
{
value_degree[i] = values[i];
Log.i("abc", "" + values[i]);
Title_value[i] = heading[i];
}
}
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Random r;
int centerX = (rect.left + rect.right) / 2;
int centerY = (rect.top + rect.bottom) / 2;
int radius = (rect.right - rect.left) / 2;
int color;
radius *= 0.5; // 1 will put the text in the border, 0 will put the text
// in the center. Play with this to set the distance of
// your text.
for (int i = 0; i < value_degree.length; i++)
{
if (i == 0)
{
r = new Random();
color = Color.argb(100, r.nextInt(256), r.nextInt(256),
r.nextInt(256));
paint.setColor(color);
Log.i("Color", "" + color);
canvas.drawArc(rectf, temp, value_degree[i], true, paint);
paint.setColor(Color.BLACK); // set this to the text color.
paint.setTextSize(12);
/*
* paint.setTextAlign(Align.CENTER);
*/float medianAngle = (temp + (value_degree[i] / 2f))
* (float) Math.PI / 180f; // this angle will place the
// text in the center of the
// arc.
canvas.drawText(Title_value[i],
(float) (centerX + (radius * Math.cos(medianAngle))),
(float) (centerY + (radius * Math.sin(medianAngle))),
paint);
} else
{
temp += value_degree[i - 1];
r = new Random();
color = Color.argb(255, r.nextInt(256), r.nextInt(256),
r.nextInt(256));
paint.setColor(color);
Log.i("Else Color", "" + color);
canvas.drawArc(rectf, temp, value_degree[i], true, paint);
paint.setColor(Color.BLACK); // set this to the text color.
paint.setTextSize(12);
/*
* paint.setTextAlign(Align.CENTER);
*/float medianAngle = (temp + (value_degree[i] / 2f))
* (float) Math.PI / 180f; // this angle will place the
// text in the center of the
// arc.
canvas.drawText(Title_value[i],
(float) (centerX + (radius * Math.cos(medianAngle))),
(float) (centerY + (radius * Math.sin(medianAngle))),
paint);
}
}
}
}
It is showing chart in good quality in small screen sizes. But when I check it on big screen like S4 or other same screen size device it is shows a pie chart in very small size.
What should I do so it will draw perfectly on small as well as big screens?
Please help me, suggest something.
Also suggest how can I take color value to each slice in pie chart as it is assigned randomly.

Categories

Resources