Android-imageswitcher
Android-イメージスイッチャー
画像が画面に突然表示されないようにしたい場合があります。むしろ、ある画像から別の画像に移行するときに画像に何らかのアニメーションを適用したい場合があります。 これは、ImageSwitcherの形式でandroidによってサポートされています。
画像スイッチャーを使用すると、画面に表示される方法で画像にトランジションを追加できます。 イメージスイッチャーを使用するには、最初にそのXMLコンポーネントを定義する必要があります。 その構文は以下のとおりです-
<ImageSwitcher
android:id="@+id/imageSwitcher1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ImageSwitcher>
ここで、javaファイルにImageSwithcerのインスタンスを作成し、このXMLコンポーネントの参照を取得します。 その構文は以下のとおりです-
private ImageSwitcher imageSwitcher;
imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher1);
次に、ViewFactoryインターフェイスを実装し、imageViewを返す未実装のメソッドを実装する必要があります。 その構文は以下です-
imageSwitcher.setImageResource(R.drawable.ic_launcher);
imageSwitcher.setFactory(new ViewFactory() {
public View makeView() {
ImageView myView = new ImageView(getApplicationContext());
return myView;
}
}
最後に行う必要があるのは、ImageSwitcherにアニメーションを追加することです。 静的メソッドloadAnimationを呼び出して、AnimationUtilitiesクラスを介してAnimationクラスのオブジェクトを定義する必要があります。 その構文は以下のとおりです-
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);
メソッドsetInAnimatonは、画面上のオブジェクトの外観のアニメーションを設定しますが、setOutAnimationはその逆を行います。 メソッドloadAnimation()は、アニメーションオブジェクトを作成します。
これらのメソッドとは別に、ImageSwitcherクラスで定義されている他のメソッドがあります。 それらは以下で定義されます-
Sr.No | Method & description |
---|---|
1 |
setImageDrawable(Drawable drawable) 画像スイッチャーで画像を設定します。 画像はビットマップの形式で渡されます |
2 |
setImageResource(int resid) 画像スイッチャーで画像を設定します。 画像は整数IDの形式で渡されます |
3 |
setImageURI(Uri uri) 画像スイッチャーで画像を設定します。 画像はURIの形式で渡されます |
4 |
ImageSwitcher(Context context, AttributeSet attrs) メソッドで渡されたいくつかの属性を設定済みの画像スイッチャーオブジェクトを返します |
5 |
onInitializeAccessibilityEvent (AccessibilityEvent event) イベントソースであるこのビューに関する情報でAccessibilityEventを初期化します |
6 |
onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info) このビューに関する情報を使用してAccessibilityNodeInfoを初期化します |
例
以下の例は、ビットマップ上の画像切り替えエフェクトの一部を示しています。 画像のアニメーション効果を表示できる基本的なアプリケーションを作成します。
この例を試すには、実際のデバイスでこれを実行する必要があります。
Steps | Description |
---|---|
1 | You will use Android studio IDE to create an Android application under a package com.example.sairamkrishna.myapplication. |
2 | Modify src/MainActivity.java file to add necessary code. |
3 | Modify the res/layout/activity_main to add respective XML components |
4 | Run the application and choose a running android device and install the application on it and verify the results |
以下は、変更されたメインアクティビティファイル src/MainActivity.java の内容です。
'_以下のコードで tp と abc はfinddevguides.comのロゴを示します_
package com.example.sairamkrishna.myapplication;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity {
private ImageSwitcher sw;
private Button b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button);
b2 = (Button) findViewById(R.id.button2);
sw = (ImageSwitcher) findViewById(R.id.imageSwitcher);
sw.setFactory(new ViewFactory() {
@Override
public View makeView() {
ImageView myView = new ImageView(getApplicationContext());
myView.setScaleType(ImageView.ScaleType.FIT_CENTER);
myView.setLayoutParams(new
ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
return myView;
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "previous Image",
Toast.LENGTH_LONG).show();
sw.setImageResource(R.drawable.abc);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Next Image",
Toast.LENGTH_LONG).show();
sw.setImageResource(R.drawable.tp);
}
});
}
}
以下は、xml res/layout/activity_main.xml の変更されたコンテンツです。
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView android:text="Gestures Example"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:id="@+id/textView"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:textColor="#ff7aff24"
android:textSize="35dp"/>
<ImageSwitcher
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageSwitcher"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="168dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/left"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/right"
android:id="@+id/button2"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button"/>
</RelativeLayout>
以下は Strings.xml ファイルの内容です。
<resources>
<string name="app_name">My Application</string>
<string name="left"><![CDATA[<]]></string>
<string name="right"><![CDATA[>]]></string>
</resources>
以下は AndroidManifest.xml ファイルの内容です。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication"
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sairamkrishna.myapplication.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
変更したアプリケーションを実行してみましょう。 環境設定中に AVD を作成したと思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 AndroidスタジオはAVDにアプリをインストールして起動し、セットアップとアプリケーションで問題がなければ、次のエミュレータウィンドウが表示されます-
Android Image Switcher Tutorial
デバイスの画面を見ると、2つのボタンが表示されています。
右矢印の上のボタンを選択してください。 画像は右から表示され、左に移動します。 以下に示されています-
Android Image Switcher Tutorial
次に、下のボタンをタップします。これにより、前の画像にいくつかのトランジションが戻ります。 以下に示されています-