主类:IndexAnimationLinearLayout.java
package com.yw.sortlistview; import java.util.ArrayList; import java.util.List; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; import android.os.Handler; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import com.yw.sortlistview.bean.AnimationBean; /** * 移动动画 * * @author tony * */ @SuppressLint("NewApi") public class IndexAnimationLinearLayout extends LinearLayout { // 外层循环 private boolean flag = true; // 内层if private boolean flagIf = true; private Context context; private List<AnimationBean> datas = new ArrayList<AnimationBean>(); public IndexAnimationLinearLayout(Context context) { super(context); this.context = context; } public IndexAnimationLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } public IndexAnimationLinearLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; } public void setResource(List<AnimationBean> datas){ this.datas = datas; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); } // 粗略 private int var = 5; /** * 移动子控件 */ public void moveChild() { // 获取布局中的控件个数 int count = this.getChildCount(); View first = this.getChildAt(0); for (int i = 0; i < count; i++) { if (first.getRight() <= 0) { this.removeView(first); this.addView(first, this.getChildCount()); onStop(); /** * 控件停止滚动时切换到不同的视图 */ if (callback != null) { callback.stop(); } } else { /* * 左、上、右、下 控制上下不变,左右改变 */ View view = this.getChildAt(i); view.layout(view.getLeft() - var, view.getTop(), view.getRight() - var, view.getBottom()); // 如果view不再Layout范围,则移除 Log.e("view.getRight", view.getRight() + ""); Log.e("this.getLeft", this.getLeft() + ""); } } } public void start(int w) { //向集合中添加数据 if(datas != null && datas.size()>0){ for(int i=0;i<datas.size();i++){ Log.e("startview", "startview"); ImageView img = (ImageView)LayoutInflater.from(context).inflate(R.layout.item, null); img.setImageResource(datas.get(i).getResId()); /*img.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));*/ img.setLayoutParams(new LinearLayout.LayoutParams( w, w)); Log.e("endview", "endview"); Log.e("resid", datas.get(i).getResId()+"dd"); this.addView(img); } } new Thread() { public void run() { try { while (flag) { if (flagIf) { Thread.sleep(200); handler.sendEmptyMessage(0); } } } catch (Exception e) { e.printStackTrace(); } }; }.start(); } public void stop() { flagIf = false; flag = false; } private void onStop() { // Toast.makeText(context, "暂停三秒试试看", Toast.LENGTH_LONG).show(); new Thread() { public void run() { try { flagIf = false; Thread.sleep(2000); flagIf = true; } catch (Exception e) { e.printStackTrace(); } }; }.start(); } Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case 0: moveChild(); break; } }; }; private MyLinearLayoutCallBack callback; public void setMyLinearLayoutCallBack(MyLinearLayoutCallBack callback) { this.callback = callback; } public interface MyLinearLayoutCallBack { public void stop(); } }
使用类:LayoutAnimationActivity.java
package com.yw.sortlistview; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.DisplayMetrics; import android.util.Log; import android.view.Display; import android.view.WindowManager; import android.widget.TextView; import android.widget.Toast; import com.yw.sortlistview.IndexAnimationLinearLayout.MyLinearLayoutCallBack; import com.yw.sortlistview.bean.AnimationBean; /** * 控件循环滚动 * * @author tony * */ public class LayoutAnimationActivity extends Activity implements MyLinearLayoutCallBack { private IndexAnimationLinearLayout linear; private TextView tv_title; private List<AnimationBean> datas = new ArrayList<AnimationBean>(); private int w = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layoutanimation_layout); linear = (IndexAnimationLinearLayout) findViewById(R.id.layoutanimation_linear); linear.setMyLinearLayoutCallBack(this); tv_title = (TextView)findViewById(R.id.layoutanimation_tv_title); getScreenHW(this); } public void getScreenHW(Context context){ WindowManager manager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); Display display = manager.getDefaultDisplay(); int width =display.getWidth(); int height=display.getHeight(); w = (int)width/8; } /** * 开始动画 */ @Override protected void onResume() { super.onResume(); for(int i=0;i<10;i++){ AnimationBean bean = new AnimationBean(); bean.setId(i+""); bean.setResId(R.drawable.ic_launcher); datas.add(bean); } linear.setResource(datas); linear.start(w); } /** * 暂停动画 */ protected void onStop() { super.onStop(); linear.stop(); } /** * 动画停止时的回调函数 */ @Override public void stop() { tv_title.setText(""); Toast.makeText(this, "暂停三秒试试看", Toast.LENGTH_LONG).show(); }; }
在xml中的使用方法:
<?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:gravity="center" android:orientation="vertical" > <com.yw.sortlistview.IndexAnimationLinearLayout android:id="@+id/layoutanimation_linear" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffff99" android:orientation="horizontal" > </com.yw.sortlistview.IndexAnimationLinearLayout> <TextView android:id="@+id/layoutanimation_tv_title" android:layout_width="300dp" android:layout_height="wrap_content" android:gravity="center" android:layout_marginTop="10dp" android:text="第一个" /> </LinearLayout>
结束。
时间: 2024-10-07 21:48:58