Android开发AnimationDrawable控制逐帧播放动画

 更新时间:2016年10月2日 16:24  点击:1817
下面我们一起来看篇Android开发AnimationDrawable控制逐帧播放动画实现过程,希望文章对各位朋友带不一些帮助。

当我们点击按钮时,该图片会不停的旋转,当再次点击按钮时,会停止在当前的状态。

activity代码:

 

 代码如下 复制代码

package cn.com.chenzheng_java.animation;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
/**
* @description android中的逐帧动画.
* 逐帧动画的原理很简单,跟电影的播放一样,一张张类似的图片不停的切换,当切换速度达到一定值时,
* 我们的视觉就会出现残影,残影的出现保证了视觉上变化的连续性,这时候图片的切换看在我们眼中就跟真实的一样了。
* 想使用逐帧动画:
* 第一步:需要在res/drawable文件夹下新建一个xml文件,该文件详细定义了动画播放时所用的图片、切换每张图片
*        所用的时间、是否为连续播放等等。(有些文章说,在res/anim文件夹下放置该文件,事实证明,会出错哦)
* 第二步:在代码中,将该动画布局文件,赋值给特定的图片展示控件,如本例子中的ImageView。
* 第三步:通过imageView.getBackGround()获取相应的AnimationDrawable对象,然后通过该对象的方法进行控制动画
* @author chenzheng_java
*
*/
public class Animation1Activity extends Activity {
ImageView imageView ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animation1);

imageView = (ImageView) findViewById(R.id.imageView_animation1);
imageView.setBackgroundResource(R.drawable.animation1_drawable);

}

public void myClickHandler(View targetButton){
// 获取AnimationDrawable对象
AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();

// 动画是否正在运行
if(animationDrawable.isRunning()){
//停止动画播放
animationDrawable.stop();
}
else{
//开始或者继续动画播放
animationDrawable.start();
}

}
}

 

animation1.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:orientation="vertical">
<Button  android:id="@+id/button_animation1" android:text="动画开始"
android:layout_gravity="center_horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:onClick="myClickHandler"></Button>
<ImageView android:id="@+id/imageView_animation1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"></ImageView>
</LinearLayout>

存放动画文件的xml文件:

 

 代码如下 复制代码
<?xml version="1.0" encoding="utf-8"?>
<!--
根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
根标签下,通过item标签对动画中的每一个图片进行声明
android:duration 表示展示所用的该图片的时间长度
-->
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false"
>
<item android:drawable="@drawable/a1" android:duration="50"></item>
<item android:drawable="@drawable/a2" android:duration="50"></item>
<item android:drawable="@drawable/a3" android:duration="50"></item>
<item android:drawable="@drawable/a4" android:duration="50"></item>
<item android:drawable="@drawable/a5" android:duration="50"></item>
<item android:drawable="@drawable/a6" android:duration="50"></item>
</animation-list>

除此之外:在AnimationDrawable中,我们还可以看到如下的几个重要方法:

setOneShot(boolean flag) 和在配置文件中进行配置一样,可以设置动画是否播放一次,false为连续播放;

addFrame (Drawable frame, int duration) 动态的添加一个图片进入该动画中

这里我们给大家总结了下关于Android TextView文本文字的常用两种应用,一种是像我们使用微信会看到长文件是可以折叠显示了,还有一种就是TextView文字颜色TextColor焦点效果,下面我一起来看这两种方法。

textview文字状态一,TextView文字颜色TextColor焦点效果

 代码如下 复制代码

<TextView

android:id="@+id/tv_quit"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textColor="@drawable/list_item_color" />

 

list_item_color 文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- 单击选中时字体颜色-->

<item android:state_pressed="true" android:color="#FFFFFF" />

<!-- 有焦点时的字体颜色-->

<item android:state_focused="true" android:color="#FFFFFF" />

<!-- 滚动选中时字体颜色-->
<item android:state_selected="true" android:color="#FFFFFF" />

<!-- 默认字体颜色-->

<item android:color="#000000" />

</selector>


textview文字状态二,Android TextView文本折叠效果

本例要实现文本展开收起的效果,即默认只显示4行文字,如果textview文字超过4行的话,点击右下角的 更多 按钮即可查看全部的内容。之前的做法是根据 TextView 中的字数来判断,效果不太好。这里在一个FrameLayout 包裹两个 TextView

布局文件 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"
android:padding="10dp"
tools:context=".MainActivity" >

<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="@string/textview_fold" />

<FrameLayout 
        android:id="@+id/fl_desc" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/tv_title" 
        android:fadingEdge="horizontal" 
        android:fadingEdgeLength="5dp" > 

        <TextView 
            android:id="@+id/tv_desc_short" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:maxLines="4" 
            android:textColor="@color/black" 
            android:textSize="16sp" /> 

        <TextView 
            android:id="@+id/tv_desc_long" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:textColor="@color/black" 
            android:textSize="16sp" /> 
    </FrameLayout> 

<Button
android:id="@+id/bt_more"
android:layout_width="50dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_below="@id/fl_desc"
android:layout_marginRight="10dp"
android:background="#1c000000"
android:gravity="center"
android:text="@string/label_more"
android:textSize="15sp"
android:visibility="gone" />

<ImageView
android:id="@+id/iv_more_line"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/bt_more"
android:layout_below="@id/fl_desc"
android:layout_toLeftOf="@id/bt_more"
android:background="@drawable/more_line"
android:contentDescription="@string/app_name"
android:visibility="gone" />

</RelativeLayout>

MainActivity.java

 

 代码如下 复制代码

package com.example.textviewfold;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

private Button bt_more;
private FrameLayout fl_desc;
private TextView tv_desc_short;
private TextView tv_desc_long;
private boolean isInit = false;
private boolean isShowShortText = true;
private ImageView iv_more_line;

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

findView();
initView();
setListener();
}

private void setListener() {
bt_more.setOnClickListener(this);
}

private void initView() {
String content = " 新浪科技讯 北京时间7月25日凌晨消息,在今天举行的新产品发布会上,谷歌发布Android 4.3版本,代号仍为"果冻豆 (Jelly Bean)"。今天发布的新一代Nexus 7将搭载该操作系统,Nexus系列设备今日可收到OTA推送更新。 Android 4.3操作系统新增一系列功能。首先是多用户设置功能,包括针对保护儿童的“受限文件(restricted profiles)” 特性。用户可以对应用内容进行限制,防止儿童在使用应用时看到不适宜内容,或接触不合适的应用内购买广告。这项功能与微软Windows Phone 的"儿童乐园(Microsoft's Kid's Corner)"功能类似。第二项升级是智能蓝牙(Bluetooth Smart)功 能,即"低功耗蓝牙(Bluetooth Low Energy)"。";
tv_desc_short.setText(content);
tv_desc_long.setText(content);

ViewTreeObserver vto = fl_desc.getViewTreeObserver();
vto.addOnPreDrawListener(new OnPreDrawListener() {
@Override
public boolean onPreDraw() {
if (isInit)
return true;
if (mesureDescription(tv_desc_short, tv_desc_long)) {
iv_more_line.setVisibility(View.VISIBLE);
bt_more.setVisibility(View.VISIBLE);
}
isInit = true;
return true;
}
});
}

/**
* 计算描述信息是否过长
*/
private boolean mesureDescription(TextView shortView, TextView longView) {
final int shortHeight = shortView.getHeight();
final int longHeight = longView.getHeight();
if (longHeight > shortHeight) {
shortView.setVisibility(View.VISIBLE);
longView.setVisibility(View.GONE);
return true;
}
shortView.setVisibility(View.GONE);
longView.setVisibility(View.VISIBLE);
return false;
}

private void findView() {
fl_desc = (FrameLayout) findViewById(R.id.fl_desc);
tv_desc_short = (TextView) findViewById(R.id.tv_desc_short);
tv_desc_long = (TextView) findViewById(R.id.tv_desc_long);

bt_more = (Button) findViewById(R.id.bt_more);
iv_more_line = (ImageView) findViewById(R.id.iv_more_line);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_more:
  if (isShowShortText) { 

                tv_desc_short.setVisibility(View.GONE); 
                tv_desc_long.setVisibility(View.VISIBLE); 
            } else { 
                tv_desc_short.setVisibility(View.VISIBLE); 
                tv_desc_long.setVisibility(View.GONE); 
            } 
toogleMoreButton(bt_more);
isShowShortText = !isShowShortText;
break;

default:
break;
}
}

/**
* 更改按钮【更多】的文本
*/
private void toogleMoreButton(Button btn) {

String text = (String) btn.getText();
String moreText = getString(R.string.label_more);
String lessText = getString(R.string.label_less);
if (moreText.equals(text)) {
btn.setText(lessText);
} else {
btn.setText(moreText);
}
}
}

 

运行效果

为展开的状态

展开后的状态

 

在这里我们来看两个关于在android开发中checkbox自定义图片的问题,一个是CheckBox自定义图片没有效果,而另一个是CheckBox自定义图片大小设置。

一,CheckBox自定义图片问题

结果点击的时候 会有checked效果,但是,手指离开后没有check住~

 代码如下 复制代码

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_checked="false"
android:drawable="@drawable/checkbox_tishi" />
<item android:state_checked="true"
android:drawable="@drawable/checkbox_tishi_select" />

</selector>

上面的写法,最后当然没好使,于是就搜索了一下,搜来的多了一对标签状态,但还是不能用。但是原生的是可以的,想了想还是看看源码好了。发现源码中的配置多了不少,当然首先就是全部复制过来。然后,适当精简一下。再次发布程序,顿时好使了。结果如下:

 代码如下 复制代码

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_checked="true" android:state_window_focused="false"
android:drawable="@drawable/checkbox_tishi_select" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="@drawable/checkbox_tishi" />

<item android:state_checked="true" android:state_pressed="true"
android:drawable="@drawable/checkbox_tishi_select" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="@drawable/checkbox_tishi" />

<item android:state_checked="true" android:state_focused="true"
android:drawable="@drawable/checkbox_tishi_select" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="@drawable/checkbox_tishi" />

<item android:state_checked="false"
android:drawable="@drawable/checkbox_tishi" />
<item android:state_checked="true"
android:drawable="@drawable/checkbox_tishi_select" />

</selector>


二,CheckBox自定义图片大小问题


1.在drawable中创建文件checkbox_selector.xml:

 代码如下 复制代码

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_checked="true"
    android:drawable="@drawable/checkbox_ok" /><!--设置选中图片-->
 <item android:state_checked="false"
   android:drawable="@drawable/checkbox_empty" /><!--设置未选中图片-->
</selector>2.在values中创建styles.xml:

<?xml version="1.0" encoding="utf-8"?>

<resources>

   <style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">

    <item name="android:button">@drawable/checkbox_selector</item>

    <item name="android:paddingLeft">25.0dip</item>

    <item name="android:maxHeight">10.0dip</item>

   </style>

</resources>

3.在你的CheckBox中添加属性:

 代码如下 复制代码

<CheckBox
        android:id="@+id/check"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginLeft="5dp"
        style="@style/MyCheckBox" 
        />

下面我们来看一款在Android开发之为按钮添加音效实例,如果你有兴趣不防进入参考。

1.layout布局文件:

 代码如下 复制代码

<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:background=”@drawable/activity_background” >

<TextView
android:id=”@+id/title_bar”
android:layout_width=”fill_parent”
android:layout_height=”45dip”
android:layout_alignParentTop=”true”
android:gravity=”center_vertical|center_horizontal”
android:background=”@drawable/theme_title_red”
android:textSize=”25dip”
android:textColor=”#fff”
android:text=”按键声测试”
tools:context=”.MainActivity” />
<Button
android:id=”@+id/btn_test”
android:layout_width=”80dip”
android:layout_height=”30dip”
android:layout_below=”@+id/title_bar”
android:layout_marginLeft=”100dip”
android:layout_marginTop=”20dip”
android:background=”@drawable/bg_btn_submit_selecter”
/>

</RelativeLayout>

 

2.Activity文件:

 代码如下 复制代码

package com.jun.activity;

import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
public Button btn=null;
private SoundPool sp;//声明一个SoundPool
private int music;//定义一个整型用load();来设置suondID

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

private void init() {
// TODO Auto-generated method stub
btn=(Button) findViewById(R.id.btn_test);
sp= new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);//第一个参数为同时播放数据流的最大个数,第二数据流类型,第三为声音质量
music = sp.load(this, R.raw.key_sound, 1); //把你的声音素材放到res/raw里,第2个参数即为资源文件,第3个为音乐的优先级

btn.setOnClickListener(listener);
}
private OnClickListener listener =new OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
sp.play(music, 1, 1, 0, 0, 1);

}};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

3.点击提交按钮,就会自动播放, /src/raw文件夹下的key_sound.mp3音乐文件!!!!!

一篇Android学习笔记之多界面切换实例,希望对各位朋友有所帮助。

用过VB 、 VC#的朋友都知道,在VB或VC#里要进行窗口(界面)切换很容易

例如在VB、C#里:

     有 Fom1、 Form2、Form3,如果要切换。

     Form1.show()

     Form2.show()

相当简单,当时在Android里, 要想这么做,很真实很“难”了。要得动动脑筋了。

 

按照我以往的编程经验,我这里提供一种简单的解决办法。

 

第一步 新建一个工程,例如“UITest”。

第二步 对这个工程的代码文件进行改造

  打开“UITestActivity.java”这个文件。将里面部分的代码删除。

把 setContentView 这一行删除掉。这样的话程序就不会自动main.xml 这个界面

 

第三步 对界面进行包装组合

   1个xml的界面文件配合一个java文件

首先我们将Main.xml这个布局界面进行“包装”

 

这样包装完毕后,我们回到UITestActivity.java 这个文件

进行修改

看到没有, 是不是很感觉像VB或VC#了。呵呵

 

第四步 再新建一个xml界面(phone.xml)

 

界面布局好后, 在新建一个类文件 FrmPhone.java

 

这一步完成后,我们回到 Main这个界面, 在里面添加一个【按钮】

按钮控件添加好后, 转到 FrmMain.java里,为这个按钮绑定事件

 

现在测试运行看看。点击Main界面的 【打开FrmPhone】就可以打开Phone这个界面了

点击后,程序跳转到 phone.xml 界面了

 

现在给 【返回】按钮再绑定一个【事件】就可以回到主界面了

 

测试看看, 是不是已经可以自由的在界面直接跳转了。而且很像VB或VC#。

[!--infotagslink--]

相关文章

  • ps动态环绕动画效果怎么制作

    ps动态环绕动画效果是现在很多人都非常喜欢的,大多数人还不知道ps动态环绕动画效果怎么制作下面文章就给大家介绍下ps怎么制作科技感十足的动态环绕动画效果,一起来看看...2017-07-06
  • Android子控件超出父控件的范围显示出来方法

    下面我们来看一篇关于Android子控件超出父控件的范围显示出来方法,希望这篇文章能够帮助到各位朋友,有碰到此问题的朋友可以进来看看哦。 <RelativeLayout xmlns:an...2016-10-02
  • 如何使用JavaScript实现无缝滚动自动播放轮播图效果

    这篇文章主要介绍了如何使用JavaScript实现“无缝滚动 自动播放”轮播图效果,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-08-20
  • Android开发中findViewById()函数用法与简化

    findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20
  • Android模拟器上模拟来电和短信配置

    如果我们的项目需要做来电及短信的功能,那么我们就得在Android模拟器开发这些功能,本来就来告诉我们如何在Android模拟器上模拟来电及来短信的功能。 在Android模拟...2016-09-20
  • 夜神android模拟器设置代理的方法

    夜神android模拟器如何设置代理呢?对于这个问题其实操作起来是非常的简单,下面小编来为各位详细介绍夜神android模拟器设置代理的方法,希望例子能够帮助到各位。 app...2016-09-20
  • android自定义动态设置Button样式【很常用】

    为了增强android应用的用户体验,我们可以在一些Button按钮上自定义动态的设置一些样式,比如交互时改变字体、颜色、背景图等。 今天来看一个通过重写Button来动态实...2016-09-20
  • Android WebView加载html5页面实例教程

    如果我们要在Android应用APP中加载html5页面,我们可以使用WebView,本文我们分享两个WebView加载html5页面实例应用。 实例一:WebView加载html5实现炫酷引导页面大多...2016-09-20
  • 深入理解Android中View和ViewGroup

    深入理解Android中View和ViewGroup从组成架构上看,似乎ViewGroup在View之上,View需要继承ViewGroup,但实际上不是这样的。View是基类,ViewGroup是它的子类。本教程我们深...2016-09-20
  • Android自定义WebView网络视频播放控件例子

    下面我们来看一篇关于Android自定义WebView网络视频播放控件开发例子,这个文章写得非常的不错下面给各位共享一下吧。 因为业务需要,以下代码均以Youtube网站在线视...2016-10-02
  • Android用MemoryFile文件类读写进行性能优化

    java开发的Android应用,性能一直是一个大问题,,或许是Java语言本身比较消耗内存。本文我们来谈谈Android 性能优化之MemoryFile文件读写。 Android匿名共享内存对外A...2016-09-20
  • Android设置TextView竖着显示实例

    TextView默认是横着显示了,今天我们一起来看看Android设置TextView竖着显示如何来实现吧,今天我们就一起来看看操作细节,具体的如下所示。 在开发Android程序的时候,...2016-10-02
  • android.os.BinderProxy cannot be cast to com解决办法

    本文章来给大家介绍关于android.os.BinderProxy cannot be cast to com解决办法,希望此文章对各位有帮助呀。 Android在绑定服务的时候出现java.lang.ClassCastExc...2016-09-20
  • Android 实现钉钉自动打卡功能

    这篇文章主要介绍了Android 实现钉钉自动打卡功能的步骤,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下...2021-03-15
  • Android 开发之布局细节对比:RTL模式

    下面我们来看一篇关于Android 开发之布局细节对比:RTL模式 ,希望这篇文章对各位同学会带来帮助,具体的细节如下介绍。 前言 讲真,好久没写博客了,2016都过了一半了,赶紧...2016-10-02
  • 微信小程序实现登录页云层漂浮的动画效果

    微信小程序目前的火热程度相信不用多言,最近利用空余时间用小程序实现了个动态的登录页效果,所以下面这篇文章主要给大家介绍了利用微信小程序实现登录页云层漂浮动画效果的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。...2017-05-09
  • Android中使用SDcard进行文件的读取方法

    首先如果要在程序中使用sdcard进行存储,我们必须要在AndroidManifset.xml文件进行下面的权限设置: 在AndroidManifest.xml中加入访问SDCard的权限如下: <!--...2016-09-20
  • Android开发之PhoneGap打包及错误解决办法

    下面来给各位简单的介绍一下关于Android开发之PhoneGap打包及错误解决办法,希望碰到此类问题的同学可进入参考一下哦。 在我安装、配置好PhoneGap项目的所有依赖...2016-09-20
  • 详解vue过度效果与动画transition使用示例

    Vue 在插入、更新或者移除 DOM 时,提供多种不同方式的应用过渡效果,Vue 提供了内置的过渡封装组件transition,该组件用于包裹要实现过渡效果的组件...2021-10-10
  • jQuery动画效果相关方法实例分析

    这篇文章主要介绍了jQuery动画效果相关方法,结合实例形式较为详细的分析了jQuery实现动画效果所用到的常见方法与相关注意事项,需要的朋友可以参考下...2016-01-05