Circle animation in different directions with Canvas and TimerTask - android

How to make that circles rotate in different directions?I have to make a Solar system animation.In the center is Sun and the planet rotate around sun with different direction and speed.Problem is that all circles are turning in clockwise directions and the degrees in rotate() method is negative for blue and gray circle...Thanks in advance
public class MainActivity extends AppCompatActivity {
ImageView imageView;
Timer timer;
Canvas canvas;
Paint paint, paint1, paint2, paint3, paint4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap bitmap = Bitmap.createBitmap(1000, 1500, Bitmap.Config.ARGB_8888);
bitmap.eraseColor(Color.parseColor("#EBEBEB"));
canvas = new Canvas();
canvas.setBitmap(bitmap);
paint = new Paint();
paint1 = new Paint();
paint2 = new Paint();
paint3 = new Paint();
paint4 = new Paint();
paint.setColor(Color.YELLOW);
paint1.setColor(Color.RED);
paint2.setColor(Color.BLUE);
paint3.setColor(Color.GREEN);
paint4.setColor(Color.GRAY);
canvas.drawCircle(500f, 750f, 130, paint);
canvas.drawCircle(400f, 560f, 51, paint1);
canvas.drawCircle(750f, 920f, 48, paint2);
canvas.drawCircle(300f, 1100f, 45, paint3);
canvas.drawCircle(320f, 300f, 42, paint4);
imageView = findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(
new Runnable() {
#Override
public void run() {
animationFrame();
}
}
);
}
}, 3000, 80);
}
private void animationFrame() {
canvas.drawColor(Color.parseColor("#EBEBEB"));
paint.setColor(Color.YELLOW);
canvas.drawCircle(500f, 750f, 130f, paint);
if (paint1 != null | paint3 != null) {
paint1.setColor(Color.RED);
canvas.drawCircle(400f, 560f, 51f, paint1);
canvas.rotate(10f, 500f, 750f);
paint3.setColor(Color.GREEN);
canvas.drawCircle(350f, 1050f, 45, paint3);
canvas.rotate(30f, 500f, 750f);
}
if (paint2 != null | paint4 != null) {
paint2.setColor(Color.BLUE);
canvas.drawCircle(720f, 950f, 48, paint2);
canvas.rotate(-10f, 500f, 750f);
paint4.setColor(Color.GRAY);
canvas.drawCircle(290f, 330f, 42, paint4);
canvas.rotate(-25f, 500f, 750f);
}
imageView.invalidate();
}

Related

draw retangular and scale to fit screen

This is my little app. It looks perfect in my device but when I use it in another device with another size screen the rectangle does not fit the screen to the edge.I try to convert pix to dp but it does not work great.
public class MyView extends View {
private static final String TAG = "MyActivity";
Paint paint;
private Rect rectangle;
Paint paint2;
private Rect rectangle2;
Paint paint3;
private Rect rectangle3;
Paint paint4;
private Rect rectangle4;
Paint paint5;
private Rect rectangle5;
Paint paint6;
private Rect rectangle6;
Paint paint7;
private Rect rectangle7;
Paint paint8;
private Rect rectangle8;
Paint paint9;
private Rect rectangle9;
Paint paint10;
private Rect rectangle10;
Paint paint11;
private Rect rectangle11;
Paint paint12;
private Rect rectangle12;
Paint paint13;
private Rect rectangle13;
Paint paint14;
private Rect rectangle14;
Paint paint15;
private Rect rectangle15;
Paint paint16;
private Rect rectangle16;
Paint colorPicker1;
private Rect rectangleRed;
Paint colorPicker2;
private Rect rectangleYellow;
Paint colorPicker3;
private Rect rectangleBlue;
Paint colorPicker4;
private Rect rectangleWhite;
int xStored;
int yStored;
int colorStore1;
public MyView(Context context) {
super(context);
init();
}
private void init(){
Log.i(TAG, "hola "+ screen);
paint = new Paint();
paint.setColor(Color.WHITE);
rectangle = new Rect(0, 0, 100, 100);
paint2 = new Paint();
paint2.setColor(Color.WHITE);
rectangle2 = new Rect(110, 0, 250, 100);
paint3 = new Paint();
paint3.setColor(Color.WHITE);
rectangle3 = new Rect(260, 0, 340, 100);
paint4 = new Paint();
paint4.setColor(Color.WHITE);
rectangle4 = new Rect(350, 0, 480, 100);
paint5 = new Paint();
paint5.setColor(Color.WHITE);
rectangle5 = new Rect(0, 110, 100, 260);
paint6 = new Paint();
paint6.setColor(Color.WHITE);
rectangle6 = new Rect(110, 110, 250, 260);
paint7 = new Paint();
paint7.setColor(Color.WHITE);
rectangle7 = new Rect(260, 110, 340, 260);
paint8 = new Paint();
paint8.setColor(Color.WHITE);
rectangle8 = new Rect(350, 110, 480, 260);
paint9 = new Paint();
paint9.setColor(Color.WHITE);
rectangle9 = new Rect(0, 270, 100, 340);
paint10 = new Paint();
paint10.setColor(Color.WHITE);
rectangle10 = new Rect(110, 270, 250, 340);
paint11 = new Paint();
paint11.setColor(Color.WHITE);
rectangle11 = new Rect(260, 270, 340, 340);
paint12 = new Paint();
paint12.setColor(Color.WHITE);
rectangle12 = new Rect(350, 270, 480, 340);
paint13 = new Paint();
paint13.setColor(Color.WHITE);
rectangle13 = new Rect(0, 350, 100, 440);
paint14 = new Paint();
paint14.setColor(Color.WHITE);
rectangle14 = new Rect(110, 350, 250, 440);
paint15 = new Paint();
paint15.setColor(Color.WHITE);
rectangle15 = new Rect(260, 350, 340, 440);
paint16 = new Paint();
paint16.setColor(Color.WHITE);
rectangle16 = new Rect(350, 350, 480, 440);
colorPicker1 = new Paint();
colorPicker1.setColor(Color.RED);
rectangleRed= new Rect(0, 580 , 120+ screen, 680);
colorPicker2 = new Paint();
colorPicker2.setColor(Color.YELLOW);
rectangleYellow = new Rect(120+ screen, 580, 240+ screen2, 680);
colorPicker3 = new Paint();
colorPicker3.setColor(Color.BLUE);
rectangleBlue = new Rect(240+ screen3, 580, 360,680 );
colorPicker4 = new Paint();
colorPicker4.setColor(Color.WHITE);
rectangleWhite = new Rect(360+ screen, 580, 480+ screen, 680);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(BLACK);
canvas.drawRect(rectangle, paint);
canvas.drawRect(rectangle2, paint2);
canvas.drawRect(rectangle3, paint3);
canvas.drawRect(rectangle4, paint4);
canvas.drawRect(rectangle5, paint5);
canvas.drawRect(rectangle6, paint6);
canvas.drawRect(rectangle7, paint7);
canvas.drawRect(rectangle8, paint8);
canvas.drawRect(rectangle9, paint9);
canvas.drawRect(rectangle10, paint10);
canvas.drawRect(rectangle11, paint11);
canvas.drawRect(rectangle12, paint12);
canvas.drawRect(rectangle13, paint13);
canvas.drawRect(rectangle14, paint14);
canvas.drawRect(rectangle15, paint15);
canvas.drawRect(rectangle16, paint16);
canvas.drawRect(rectangleRed, colorPicker1);
canvas.drawRect(rectangleYellow,colorPicker2 );
canvas.drawRect(rectangleBlue,colorPicker3 );
//canvas.drawRect(rectangleWhite,colorPicker4 );
}
#Override
public boolean onTouchEvent( MotionEvent event) {
super.onTouchEvent(event);
int x = (int)event.getX();
int y = (int)event.getY();
xStored = x; yStored=y;
if (event.getAction()==MotionEvent.ACTION_UP){
}else if(event.getAction()==MotionEvent.ACTION_DOWN){
if(rectangleRed.contains(x,y)){
colorStore1 = colorPicker1.getColor();
}
if(rectangleYellow.contains(x,y)){
colorStore1 = colorPicker2.getColor();
}
if (rectangleBlue.contains(x,y)){
colorStore1 = colorPicker3.getColor();
}
if (rectangleWhite.contains(x,y)){
colorStore1 = colorPicker4.getColor();
}
if(rectangle.contains(x,y)){
paint.setColor(colorStore1);
}
if(rectangle2.contains(x,y)){
paint2.setColor(colorStore1);
}
if(rectangle3.contains(x,y)){
paint3.setColor(colorStore1);
}
if(rectangle4.contains(x,y)){
paint4.setColor(colorStore1);
}
if(rectangle5.contains(x,y)){
paint5.setColor(colorStore1);
}
if(rectangle6.contains(x,y)){
paint6.setColor(colorStore1);
}
if(rectangle7.contains(x,y)){
paint7.setColor(colorStore1);
}
if(rectangle8.contains(x,y)){
paint8.setColor(colorStore1);
}
if(rectangle9.contains(x,y)){
paint9.setColor(colorStore1);
}
if(rectangle10.contains(x,y)){
paint10.setColor(colorStore1);
}
if(rectangle11.contains(x,y)){
paint11.setColor(colorStore1);
}
if(rectangle12.contains(x,y)){
paint12.setColor(colorStore1);
}
if(rectangle13.contains(x,y)){
paint13.setColor(colorStore1);
}
if(rectangle14.contains(x,y)){
paint14.setColor(colorStore1);
}
if(rectangle15.contains(x,y)){
paint15.setColor(colorStore1);
}
if(rectangle16.contains(x,y)){
paint16.setColor(colorStore1);
}
}else if(event.getAction()==MotionEvent.ACTION_MOVE){
}
this.postInvalidate();
return true;
}
}
Use the getWidth() and getHeight() methods of View to get width and height at runtime, matching the actual screen.

How to make my canvas scrollable

I draw with the code below a algorithm. But how can I make it scrollable (vertical and horizontal)?
The Problem is, that in portrait mode the drawing goes outside the display, but when I make it smaller the user cant read the text anymore.
public class legende extends View {
private Paint paint;
private RectF einstiegRectF;
private RectF massnahmenRectF;
Path arrowPath;
public legende(Context context) {
super(context);
init();
}
public void init(){
paint = new Paint();
einstiegRectF = new RectF(20, 20, 820, 220);
arrowPath = new Path();
massnahmenRectF = new RectF(20, 320, 820, 420);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
paint.setColor(Color.parseColor("#ccd2ec"));
paint.setStyle(Paint.Style.FILL);
canvas.drawOval(einstiegRectF, paint);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
canvas.drawOval(einstiegRectF, paint);
paint.setTextSize(35);
canvas.drawText("Einstiegsbedingungen / Fortsetzung von ...", 100, 130, paint);
paint.setStrokeWidth(8);
canvas.drawLine(420, 220, 420, 300, paint);
arrowPath.moveTo(420, 300);
arrowPath.lineTo(430, 300);
arrowPath.lineTo(420, 310);
arrowPath.lineTo(410, 300);
arrowPath.close();
canvas.drawPath(arrowPath, paint);
paint.setStrokeWidth(4);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(massnahmenRectF, paint);
paint.setStrokeWidth(0);
canvas.drawText("Maßnahme", 350, 382, paint);
paint.setStrokeWidth(4);
Path rectArrow = new Path();
rectArrow.moveTo(840, 370);
rectArrow.lineTo(940, 320);
rectArrow.lineTo(1240, 320);
rectArrow.lineTo(1240, 420);
rectArrow.lineTo(940, 420);
rectArrow.close();
canvas.drawPath(rectArrow, paint);
}
}

Drawing 2 circles on a canvas

I'm trying to draw two circles like this:
This is how I'm trying to do it:
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
Canvas c = new Canvas(bmp);
RectF rect = new RectF(0,0,width,width);
Paint paint = new Paint();
drawCircles(paint, c, width, height, width);
ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setImageBitmap(bmp);
img.setScaleType(ScaleType.FIT_CENTER);
And here is my drawCircles() method:
private void drawCircles(Paint paint, Canvas c, int width, int height, int radius) {
paint.setARGB(255, 255 , 10, 21);
paint.setStrokeWidth(10);
paint.setAntiAlias(true);
paint.setStrokeCap(Paint.Cap.BUTT);
paint.setStyle(Paint.Style.STROKE);
if(width < height && radius == 0){
radius = width/2;
height = width;
} else if (radius == 0){
radius = height/2;
width = height;
}
Paint paint2 = new Paint();
paint2.setARGB(255, 255 , 10, 21);
paint2.setStrokeWidth(10);
paint2.setAntiAlias(true);
paint2.setStrokeCap(Paint.Cap.BUTT);
paint2.setStyle(Paint.Style.STROKE);
c.drawCircle(width/2, height/2, radius-10, paint);
c.drawCircle(width/2, height/2, 50, paint2);
}
I don't know why but I get only one circle, the small one (the one drawn with paint2).
What can be the reason?
Try this code.Hope it may helps :)
public class SimpleCircleActivity extends Activity
{
private CircleDemoView circledemoView ;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
circledemoView =new CircleDemoView(this);
setContentView(circledemoView);
}
private class CircleDemoView extends View
{
public CircleDemoView(Context context)
{
super(context);
}
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint p = new Paint();
p.setColor(Color.RED);
DashPathEffect dashPath = new DashPathEffect(new float[]{5,5}, (float)1.0);
p.setPathEffect(dashPath);
p.setStyle(Style.STROKE);
for (int i = 0; i < 2; i ++) {
canvas.drawCircle(200, 200, 50+(i*40), p);
}
invalidate();
}
}
}

quicky_XY returns bitmap,inside loop called quicky_XY, displayed images,but only last image displayed.What's the problem

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTitle("XY Plot");
bitArray=new ArrayList<Bitmap>(s);
image = (ImageView) findViewById(R.id.image);
width = 220;
height = 220;
while(k<s)
{
Bitmap temp=quicky_XY();
image.setImageBitmap(temp); //displaying the image
k++;
try
{
Thread.sleep(500); //waiting for 500 ms
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
public Bitmap quicky_XY()
{
Bitmap Bmap1 = Bitmap.createBitmap(220,220, Config.ARGB_8888);
Canvas canvas = new Canvas(Bmap1);
final int color = 0xff0B0B61;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, width, height);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRect(rect,paint);
draw_the_grid(canvas);
getdata(); //getting the data
getmax(data_2_plot);
offsetx = 10; //do not care abt these lines till offsety
xscale = (210-offsetx )/(size-1);
yscale = (200)/(m[0] -m[1]);
offsety = (int)(10-(yscale*m[1]));
plot(canvas); //does all the drawing on canvas
canvas.drawBitmap(Bmap1, rect, rect, paint);
return Bmap1; //returning the image
}
You should write your loop in another Thread to make it run separate with Android UI thread.
For example:
Handler handler;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTitle("XY Plot");
bitArray=new ArrayList<Bitmap>(s);
image = (ImageView) findViewById(R.id.image);
handler = new Handler();
width = 220;
height = 220;
new displayImage().start();
}
class displayImage extends Thread{
public void run(){
while(k<s)
{
Bitmap temp=quicky_XY();
handler.post(new Runnable() {
public void run()
{
image.setImageBitmap(temp); //displaying the image
}
});
k++;
try
{
Thread.sleep(500); //waiting for 500 ms
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
public Bitmap quicky_XY()
{
Bitmap Bmap1 = Bitmap.createBitmap(220,220, Config.ARGB_8888);
Canvas canvas = new Canvas(Bmap1);
final int color = 0xff0B0B61;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, width, height);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRect(rect,paint);
draw_the_grid(canvas);
getdata(); //getting the data
getmax(data_2_plot);
offsetx = 10; //do not care abt these lines till offsety
xscale = (210-offsetx )/(size-1);
yscale = (200)/(m[0] -m[1]);
offsety = (int)(10-(yscale*m[1]));
plot(canvas); //does all the drawing on canvas
canvas.drawBitmap(Bmap1, rect, rect, paint);
return Bmap1; //returning the image
}

android shadow image effect

I'm trying to add shadow effect do an image. I use a mask to draw the image (I need a specific shape for my image). Can you please tell me how to add shadow effect to my image? I've tried something like paint.setShadowLayer(10, 10, 10, Color.RED) but it didn't worked. Here is the source code:
#Override
public void draw(Canvas canvas) {
Rect rect = new Rect(0, 0, getWidth() - 1, getHeight() - 1);
NinePatchDrawable mask = (NinePatchDrawable) getContext().getResources().getDrawable(maskResId);
mask.setBounds(rect);
Bitmap content = Bitmap.createBitmap(rect.width(), rect.height(), Bitmap.Config.ARGB_8888);
Canvas contentCanvas = new Canvas(content);
super.draw(contentCanvas);
Paint paint = new Paint();
paint.setXfermode(new AvoidXfermode(Color.BLACK, 255, AvoidXfermode.Mode.TARGET));
mask.draw(canvas);
canvas.drawBitmap(content, null, rect, paint);
}
my solution:
class ShadowImage
public class ShadowImage extends BitmapDrawable {
Bitmap bm;
static float shadowRadius = 4f;
static PointF shadowDirection = new PointF(2f, 2f);
int fillColor = 0;
#Override
public void draw(Canvas canvas) {
Rect rect = new Rect(0, 0, bm.getWidth(), bm.getHeight());
Log.i("TEST", rect.toString());
setBounds(rect);
Paint mShadow = new Paint();
mShadow.setAntiAlias(true);
mShadow.setShadowLayer(shadowRadius, shadowDirection.x, shadowDirection.y, Color.BLACK);
canvas.drawRect(rect, mShadow);
if(fillColor != 0) {
Paint mFill = new Paint();
mFill.setColor(fillColor);
canvas.drawRect(rect, mFill);
}
canvas.drawBitmap(bm, 0.0f, 0.0f, null);
}
public ShadowImage(Resources res, Bitmap bitmap) {
super(res, Bitmap.createScaledBitmap(bitmap, (int) (bitmap.getWidth()+shadowRadius*shadowDirection.x), (int) (bitmap.getHeight()+shadowRadius*shadowDirection.y), false));
this.bm = bitmap;
}
public ShadowImage(Resources res, Bitmap bitmap, int fillColor) {
super(res, Bitmap.createScaledBitmap(bitmap, (int) (bitmap.getWidth()+shadowRadius*shadowDirection.x), (int) (bitmap.getHeight()+shadowRadius*shadowDirection.y), false));
this.bm = bitmap;
this.fillColor = fillColor;
}
}
Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout root = (LinearLayout) findViewById(R.id.root_layout);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ShadowImage image = new ShadowImage(getResources(), bmp);
ShadowImage image2 = new ShadowImage(getResources(), bmp, Color.WHITE);
ImageView iv_normal = new ImageView(getApplicationContext());
iv_normal.setPadding(10, 10, 10, 10);
iv_normal.setImageBitmap(bmp);
ImageView iv_shadow = new ImageView(getApplicationContext());
iv_shadow.setPadding(10, 10, 10, 10);
iv_shadow.setImageDrawable(image);
ImageView iv_fill = new ImageView(getApplicationContext());
iv_fill.setPadding(10, 10, 10, 10);
iv_fill.setImageDrawable(image2);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
root.addView(iv_normal, params);
root.addView(iv_shadow, params);
root.addView(iv_fill, params);
root.setGravity(Gravity.CENTER);
}
Image:

Categories

Resources