Pages

Showing posts with label ImageView. Show all posts
Showing posts with label ImageView. Show all posts

Tuesday, September 20, 2016

Android Example-Simple Animation Activity(Car Move from Other location to one)


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"

        android:text="MOVE" />

    <ImageView
        android:id="@+id/img1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:paddingTop="40dp"
        android:layout_marginLeft="60dip"
        android:src="@drawable/car" />

    <ImageView
        android:id="@+id/img2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/car" />

    <ImageView
        android:id="@+id/img3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="100dip"
        android:layout_marginLeft="60dip"
        android:src="@drawable/car" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/img4"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_marginLeft="60dip"
            android:layout_marginTop="150dip"
            android:src="@drawable/car" />
    </LinearLayout>

</RelativeLayout>

MainActivity.java
package in.kantapp.example27;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ((Button) findViewById( R.id.btn1 )).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                ImageView img = (ImageView) findViewById( R.id.img1 );
                moveViewToScreenCenter( img );
                img = (ImageView) findViewById( R.id.img2 );
                moveViewToScreenCenter( img );
                img = (ImageView) findViewById( R.id.img3 );
                moveViewToScreenCenter( img );
                img = (ImageView) findViewById( R.id.img4 );
                moveViewToScreenCenter( img );
            }
        });
    }

    private void moveViewToScreenCenter( View view )
    {
        RelativeLayout root = (RelativeLayout) findViewById( R.id.rootLayout );
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics( dm );
        int statusBarOffset = dm.heightPixels - root.getMeasuredHeight();

        int originalPos[] = new int[2];
        view.getLocationOnScreen( originalPos );

        int xDest = dm.widthPixels/2;
        xDest -= (view.getMeasuredWidth()/2);
        int yDest = dm.heightPixels/2 - (view.getMeasuredHeight()/2) - statusBarOffset;

        TranslateAnimation anim = new TranslateAnimation( 0, xDest - originalPos[0] , 0, yDest - originalPos[1] );
        anim.setDuration(3000);
        anim.setFillAfter( true );
        view.startAnimation(anim);
    }
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.example27">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Monday, September 19, 2016

Android Example -Animate a Bitmap in X and Y direction


activity_main.xml
<RelativeLayout 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"
    tools:context=".MainActivity" >



    <ImageView android:id="@+id/animatedImage"
        android:layout_width="160dp"
        android:layout_height="160dp"
        android:layout_centerInParent="true"
        android:src="@drawable/rasenshuriken"/>
</RelativeLayout>

MainActivity.java
package in.kantapp.example20;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    animate var;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        /*setContentView(R.layout.activity_main);
        ImageView imageView= (ImageView) findViewById(R.id.animatedImage);

        Animation animation= AnimationUtils.loadAnimation(this,R.anim.roatate);
        imageView.startAnimation(animation);*/

        var = new animate(this);
        setContentView(var);
    }


}

anim/roatate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator" >

    <rotate android:duration="500"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:startOffset="0"
        android:toDegrees="360"/>
</set>

animete.java
package in.kantapp.example20;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class animate extends View{

    Bitmap bm;
    int x, y;
    public animate(Context context) {
        super(context);

        bm=BitmapFactory.decodeResource(getResources(), R.drawable.rasengan);

        x = 0; y = 0;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        Rect myrect = new Rect(0, 0, canvas.getWidth(),canvas.getHeight());

        Paint pa = new Paint();
        pa.setColor(Color.WHITE);
        pa.setStyle(Paint.Style.FILL);


        canvas.drawRect(myrect, pa);


        if (x < canvas.getWidth()) {
            x += 1;
        }
        else {
            x = 0;
        }
        if (y < canvas.getHeight()) {
            y += 15;
        }
        else {
            y = 0;
        }
        canvas.drawBitmap(bm, x, y, new Paint());
        invalidate();//calls this method again and again
    }

}



Tuesday, September 13, 2016

Android Example-Set the Wallpaper of Your Device using Bitmap Class


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:orientation="vertical"
    tools:context="in.kantapp.example110.MainActivity">

    <ImageView
        android:id="@+id/img"
        android:layout_width="fill_parent"
        android:layout_height="300dp"
        android:paddingTop="10dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/aa" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SetWallpaper" />

    <HorizontalScrollView
        android:layout_width="match_parent"

        android:layout_height="fill_parent">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/img1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="fitXY"
                android:src="@drawable/a" />

            <ImageView
                android:id="@+id/img2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:scaleType="fitXY"
                android:src="@drawable/b" />

            <ImageView
                android:id="@+id/img3"
                android:layout_width="160dp"
                android:layout_height="160dp"
                android:paddingLeft="5dp"

                android:scaleType="fitXY"
                android:src="@drawable/c" />

            <ImageView
                android:id="@+id/img4"
                android:layout_width="160dp"
                android:layout_height="160dp"
                android:paddingLeft="5dp"
                android:scaleType="fitXY"
                android:src="@drawable/d" />

            <ImageView
                android:id="@+id/img5"
                android:layout_width="160dp"
                android:layout_height="160dp"
                android:paddingLeft="5dp"
                android:scaleType="fitXY"
                
                android:src="@drawable/e" />
        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>

MainActivity.java
package in.kantapp.example110;

import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener
{
    ImageView display;
    int tophone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        // to our activity to cover the whole screen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);
        Button but = (Button) findViewById(R.id.button);
        tophone = R.drawable.aa;
        but.setOnClickListener(this);
        display = (ImageView) findViewById(R.id.img);
        ImageView image1 = (ImageView) findViewById(R.id.img1);
        ImageView image2 = (ImageView) findViewById(R.id.img2);
        ImageView image3 = (ImageView) findViewById(R.id.img3);
        ImageView image4 = (ImageView) findViewById(R.id.img4);
        ImageView image5 = (ImageView) findViewById(R.id.img5);

        image1.setOnClickListener(this);
        image2.setOnClickListener(this);
        image3.setOnClickListener(this);
        image4.setOnClickListener(this);
        image5.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Toast var;
        switch (v.getId()) {
            case R.id.img1:
                display.setImageResource(R.drawable.aa);
                var = Toast.makeText(MainActivity.this, "image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                tophone = R.drawable.aa;
                break;
            case R.id.img2:
                display.setImageResource(R.drawable.bb);
                var = Toast.makeText(MainActivity.this, "image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                tophone = R.drawable.bb;
                break;
            case R.id.img3:
                display.setImageResource(R.drawable.cc);
                var = Toast.makeText(MainActivity.this, "image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                tophone = R.drawable.cc;
                break;
            case R.id.img4:
                display.setImageResource(R.drawable.dd);
                var = Toast.makeText(MainActivity.this, "image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                tophone = R.drawable.dd;
                break;
            case R.id.img5:
                display.setImageResource(R.drawable.ee);
                var = Toast.makeText(MainActivity.this, "image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                tophone = R.drawable.ee;
                break;
            case R.id.button:
                // to set a background we need to use bitmap
                InputStream is = getResources().openRawResource(tophone);
                // to phone is a variable that is updated everytime we click on an
                // ImageView to that imageview resource and by clicking the button
                // we set the phone background to that image.
                Bitmap bm = BitmapFactory.decodeStream(is);
                // decode inputstream is
                try {
                    getApplicationContext().setWallpaper(bm);
                    // to set the wallpaper of the phone background we need to ask
                    // permission from the user so add permission of background from
                    // manifest file

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                var = Toast.makeText(MainActivity.this, "Wallpaper image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                break;
        }
    }

}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.example110">
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>