This tutorial is all about scrolling series of images automatically which are arranged vertically as well as horizontally.
Please see the video:
---------------------------------------- scroll_animation_activity.xml ----------------------------------
Please see the video:
Make sure your images are kept in the drawable folder.
<?xml version="1.0" encoding="utf-8"?>----------------------------------------------------------------------------------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorPrimaryDark">
<HorizontalScrollView
android:id="@+id/saa_horizontalSV"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/saa_hscroll_childLL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/colorPrimary">
<ImageView
android:src="@drawable/i7"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
</HorizontalScrollView>
<ScrollView
android:id="@+id/saa_simpleSV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<LinearLayout
android:id="@+id/saa_sscroll_childLL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/colorPrimary">
<ImageView
android:src="@drawable/i7"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
------------------------------------- ScrollAnimationActivity.java -------------------------------------
package com.app.yourappname;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
public class ScrollAnimationActivity extends AppCompatActivity {
private HorizontalScrollView hsv;
private ScrollView sv;
private int num=0;
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.scroll_animation_activity);
LinearLayout hscrollChildLL = findViewById(R.id.saa_hscroll_childLL);
LinearLayout sscrollChildLL = findViewById(R.id.saa_sscroll_childLL);
ImageView iv;
int imgs[] = {R.drawable.i0, R.drawable.i1, R.drawable.i2, R.drawable.i3, R.drawable.i4,
R.drawable.i5, R.drawable.i6, R.drawable.i7, R.drawable.i8, R.drawable.i9, };
for(int i=0; i<10; i++) {
iv = new ImageView(this);
iv.setImageResource(imgs[i]);
hscrollChildLL.addView(iv);
iv = new ImageView(this);
iv.setImageResource(imgs[imgs.length-(i+1)]);
sscrollChildLL.addView(iv);
}
hsv = findViewById(R.id.saa_horizontalSV);
sv = findViewById(R.id.saa_simpleSV);
autoScroll();
}
void autoScroll() {
new Thread(){
@Override
public void run() {
while(true) {
try{
Thread.sleep(25);
}catch(InterruptedException e) {}
handler.sendMessage(handler.obtainMessage());
num++;
}
}
}.start();
}
Handler handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
hsv.scrollTo(num,0);
sv.scrollTo(0,num);
}
};
}
------------------------------------------------------------------------
------------------------------- Declare the Activity Name in Manifest File -----------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.yourappname">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ScrollAnimationActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment