Create sample application that draw the circle, oval shape and square in android - android programming

No comments
Create sample application that draw the circle, oval shape and square.

Program:

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:id="@+id/activity_main"
   
android:layout_width="wrap_content"
   
android:layout_height="wrap_content"
   
android:paddingBottom="@dimen/activity_vertical_margin"
   
android:paddingLeft="@dimen/activity_horizontal_margin"
   
android:paddingRight="@dimen/activity_horizontal_margin"
   
android:paddingTop="@dimen/activity_vertical_margin"
   
tools:context="com.example.anksh.prac9.MainActivity">

    <
ImageView
       
android:layout_width="match_parent"
       
android:layout_height="200dp"
       
android:layout_marginTop="100dp"
       
android:id="@+id/imageView2"
       
android:layout_alignParentTop="true"
       
android:layout_centerHorizontal="true" />

    <
Button
       
android:text="Start"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:id="@+id/bsrt1"
       
android:layout_alignParentBottom="true"
       
android:layout_alignParentStart="true"
       
android:layout_marginBottom="17dp" />

    <
Button
       
android:text="Stop"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:id="@+id/Bsp2"
       
android:layout_marginBottom="35dp"
       
android:layout_alignParentBottom="true"
       
android:layout_alignParentEnd="true" />

</
RelativeLayout>

MainActivity.java

package com.example.anksh.prac9;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    Button
bst2,bstr1;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);

       
final ImageView gyroView = (ImageView) findViewById(R.id.imageView2);
       
bstr1=(Button)findViewById(R.id.bsrt1);
       
bst2=(Button)findViewById(R.id.Bsp2);

        gyroView.setBackgroundResource(R.drawable.
file1); //1
        //create an animation drawable using the background
       
final AnimationDrawable gyroAnimation = (AnimationDrawable) gyroView.getBackground();//2

        //start the animation
       
bstr1.setOnClickListener(new View.OnClickListener() {

           
@Override
           
public void onClick(View v) {
               
// TODO Auto-generated method stub

               
gyroView.post(new Runnable() {

                   
@Override
                   
public void run() {
                       
// TODO Auto-generated method stub
                       
gyroAnimation.start(); //3
                   
}
                });
            }
        });

       
bst2.setOnClickListener(new View.OnClickListener() {

           
@Override
           
public void onClick(View v) {
               
// TODO Auto-generated method stub
               
gyroAnimation.stop();
                System.exit(
0);
            }
        });
    }
    }

File1.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
   
android:oneshot="false"
>

    <
item
       
android:drawable="@drawable/a1"
       
android:duration="200"
/>
    <
item
       
android:drawable="@drawable/a2"
       
android:duration="100"
/>
    <
item
       
android:drawable="@drawable/a3"
       
android:duration="100"
/>
    <
item
       
android:drawable="@drawable/a4"
       
android:duration="100"
/>
    <
item
       
android:drawable="@drawable/a5"
       
android:duration="100"
/>
    <
item
       
android:drawable="@drawable/a6"
       
android:duration="100"
/>
    <
item
       
android:drawable="@drawable/a7"
       
android:duration="100"
/>
    <
item
       
android:drawable="@drawable/a8"
       
android:duration="100"
/>

</
animation-list>

Output:



No comments :

Post a Comment