Activity_main.xml
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout 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"
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:orientation="vertical"
>
</LinearLayout>
<LinearLayout 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"
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:orientation="vertical"
>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity { DrawingBall v; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); v = new DrawingBall(this); setContentView(v); } }
DrawingBall.java
package troubleshooter.royaloak.com.testone; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.view.View; /** * Created by Akash on 6/28/2016. */ public class DrawingBall extends View { Bitmap bitmap; int x,y; public DrawingBall(Context context) { super(context); bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Rect myRect = new Rect(0,0,canvas.getWidth(),canvas.getHeight()/2); Paint paint = new Paint(); paint.setColor(Color.RED); paint.setStyle(Paint.Style.FILL); canvas.drawRect(myRect, paint); x = 0; y = 0; if(x > canvas.getWidth()) { x += 10; } else { x = 0; } if( y > canvas.getHeight()) { y += 10; } else { y = 0; } Paint p = new Paint(); canvas.drawBitmap(bitmap,x,y,p); invalidate(); } }
No comments:
Post a Comment