android 给LinearLayout中添加一定数量的控件,并让着一定数量的控件从右到左移动,每隔若干秒停顿一下,最后一个view链接第一个view,然后继续移动循环往复,形成一个死循环简单动画效果
主类: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>
结束。
android 给LinearLayout中添加一定数量的控件,并让着一定数量的控件从右到左移动,每隔若干秒停顿一下,最后一个view链接第一个view,然后继续移动循环往复,形成一个死循环简单动画效果的更多相关文章
- 在Android源码树中添加userspace I2C读写工具(i2c-util)
在Android源码树中添加userspace I2C读写工具(i2c-util) http://blog.csdn.net/21cnbao/article/details/7919055 分类: A ...
- Android源码中添加C可执行程序
在Android源码中添加C/CPP可执行程序一般保存在external目录中 下面是每个文件的内容 ①add.c #include "add.h" int add (int a, ...
- 我的Android进阶之旅------>如何在多个LinearLayout中添加分隔线
如果要适合于所有的Android版本,可以在多个LinearLayout放置用于显示分隔线的View.例如,放一个ImageView组件,然后将其背景设为分隔线的颜色或图像,分隔线View的定义代码如 ...
- 在Android 源码中添加系统服务
Android系统本身提供了很多系统服务,如WindowManagerService,PowerManagerService等.下面描述一下添加一个系统服务的具体步骤. 1.定义自定义系统服务接口 撰 ...
- 如何在多个LinearLayout中添加分隔线
1.可以放置一个ImageView组件,然后将其设为分隔线的颜色或图形.分隔线View的定义代码如下: 2.在Android3.0及以上版本,LinearLayout支持直接显示分隔线. an ...
- Android源码中添加APP
参考罗升阳<Android系统源代码情景分析> 在Android源码中,我们通常把实验性质的Android APP放在packages/experimental目录下.对于一个简单的应用程 ...
- Android的LinearLayout中orientation默认值为什么是HORIZONTAL
在一个偶然(闲着无聊)的过程中,突然非常好奇为什么LinearLayout在不指定排列方向为垂直(VERTICAL)时就得是水平方向(HORIZONTAL)排列的.产生这个疑问的时候脑子里蹦出来的第一 ...
- iOS添加到购物车的简单动画效果
#pragma mark - 添加到购物车的动画效果 // huangyibiao - (void)addAnimatedWithFrame:(CGRect)frame { // 该部分动画 以sel ...
- ios开发之--简单动画效果的添加
记录一个简单的动画效果,自己写的,很简单,仅做记录. 附一个demo的下载地址: https://github.com/hgl753951/hglTest.git 代码如下: 1,准备 BOOL _i ...
随机推荐
- java 多线程简单例子
实现线程的方式是一,继承Thread类,重写父类的run()方法 二,实现接口Runnable中的run()方法. 下面是简单的例子 例子1:银行存取钱问题 package com.direct.de ...
- js 中导出excel 较长数字串会变成科学计数法
在做项目中,碰到如题的问题.比如要将居民的信息导出到excel中,居民的身份证号码因为长度过长(大于10位),excel会自动的将过长的数字串转换成 科学计数法.现在网上找到解决方案之一: (在数字串 ...
- python中传值和传地址问题
在python中,还没有对这个知识点有一个详细的定义,很模糊的说明了,通过下面代码,可以观察出来,什么时候传的是值,什么时候传的是地址 有时候会发现自己的数据发生变化,可能就是这个原因,python的 ...
- Windows下 webpack4.0 的安装
这里我们通过npm来进行安装 1. 安装 webpack // 全局安装webpack npm install webpack -g 2. 通过 webpack -v 命令查看当前安装的版本 此时如果 ...
- js,H5本地存储
//存储本地存储----setItem(存储名称,数据名称) var c={name:"man",sex:"woman"}; localStorage.setI ...
- Java并发编程(九)并发容器
并发容器的简单介绍: ConcurrentHashMap代替同步的Map(Collections.synchronized(new HashMap())),众所周知,HashMap是根据散列值分段存储 ...
- spring cloud 配置文件application.yml和bootstrap.yml 的定位,区别和联系总算是有一点明白了
最近在启用springcloud配置中心server的东西,在整理属性资源的时候,突然发现:用了这么久的springboot,为什么会配置两个属性文件同时存在(application.yml/prop ...
- python正则表达式模块re:正则表达式常用字符、常用可选标志位、group与groups、match、search、sub、split,findall、compile、特殊字符转义
本文内容: 正则表达式常用字符. 常用可选标志位. group与groups. match. search. sub. split findall. compile 特殊字符转义 一些现实例子 首发时 ...
- 10-openldap同步原理
openldap同步原理 阅读视图 openldap同步原理 syncrepl.slurpd同步机制优缺点 OpenLDAP同步条件 OpenLDAP同步参数 1. openldap同步原理 Open ...
- 利用Spring的AbstractRoutingDataSource解决多数据源的问题
多数据源问题很常见,例如读写分离数据库配置. 原来的项目出现了新需求,局方要求新增某服务器用以提供某代码,涉及到多数据源的问题. 解决方法如下: 1.首先配置多个datasource <bean ...