由於公司要求項目裏面要有一些動畫,於是自己就研究了一些。先上圖看看效果
首先第一個動畫
第一個動畫其實就是我們通常使用的AlphaAnimation,ScaleAnimation進行的透明度的變化和放大縮小的動畫。點的閃動就是我們使用的放大縮小的動畫代碼爲
ScaleAnimation sa = new ScaleAnimation(1.0f, 0.9f, 1.0f, 0.9f,
// 設置錨點
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// 顯示時間
sa.setDuration(time);
// 界面停留在結束狀態
sa.setFillAfter(true);
view.startAnimation(sa);
}
private void scaleBlg(ImageView view,int time){
ScaleAnimation sa = new ScaleAnimation(0.9f, 1.0f, 0.9f, 1.0f,
// 設置錨點
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// 顯示時間
sa.setDuration(time);
// 界面停留在結束狀態
sa.setFillAfter(true);
view.startAnimation(sa);
}
只是有上面的代碼還是不行的,還有有一個閃動的時間控制,這個就是有Handler來處理了,具體代碼爲
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
public void run() {
handler.postDelayed(this, 400);
if (!isFlash){
scaleTime++;
if(scaleTime%2==1){
scaleBlg(img_notice,400);
}else{
scaleSmall(img_notice,400);
}
}
}
};
點擊點以後點的動畫就要消失了,進而顯示點點擊後的透明變化的效果代碼爲
private void startAlpha(View view){
AlphaAnimation aa = new AlphaAnimation(0f, 1.0f);
// 設置動畫播放的時間 毫秒爲單位
aa.setDuration(4000);
// 界面停留在動畫結束狀態
aa.setFillAfter(true);
view.startAnimation(aa);
}
第二個動畫是具有旋轉和放大縮小的效果的,這裏也不多介紹了上代碼吧
package demo.liuyongxiang.com.demo.activities;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import demo.liuyongxiang.com.demo.R;
import demo.liuyongxiang.com.demo.view.CircleImageView;
import demo.liuyongxiang.com.demo.view.CircleLayout;
/**
* 圓形旋轉菜單(自定義Layout+ImageView)
* @author Administrator
*
*/
public class Anim2Activity extends Activity implements View.OnClickListener,CircleLayout.OnItemSelectedListener, CircleLayout.OnItemClickListener {
private CircleLayout circleMenu;
private CircleImageView img_firefoxt;
private CircleImageView img_ubuntu;
private CircleImageView img_timer;
private String selectName;
List<String> mlist = new ArrayList<>();
private Handler handler = new Handler();
int scaleTime = 0;
private Runnable runnable = new Runnable() {
public void run() {
handler.postDelayed(this, 2 * 1000);
scaleTime++;
if(scaleTime%2==1){
scaleBlg(img_firefoxt,400);
}else{
scaleSmall(img_firefoxt,400);
}
if(scaleTime%2==1){
scaleSmall(img_ubuntu,300);
}else{
scaleBlg(img_ubuntu,300);
}
if(scaleTime%2==1){
scaleSmall(img_timer,500);
}else{
scaleBlg(img_timer,500);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_anim2);
initView();
initEvent();
}
private void scaleSmall(CircleImageView view,int time){
ScaleAnimation sa = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f,
// 設置錨點
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// 顯示時間
sa.setDuration(time);
// 界面停留在結束狀態
sa.setFillAfter(true);
view.startAnimation(sa);
}
private void scaleBlg(CircleImageView view,int time){
ScaleAnimation sa = new ScaleAnimation(0.5f, 1.0f, 0.5f, 1.0f,
// 設置錨點
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// 顯示時間
sa.setDuration(time);
// 界面停留在結束狀態
sa.setFillAfter(true);
view.startAnimation(sa);
}
public void initView() {
circleMenu = (CircleLayout)findViewById(R.id.main_circle_layout);
img_firefoxt = (CircleImageView) findViewById(R.id.img_firefoxt);
img_ubuntu = (CircleImageView) findViewById(R.id.img_ubuntu);
img_timer = (CircleImageView) findViewById(R.id.img_timer);
handler.removeCallbacks(runnable);
handler.postDelayed(runnable, 2 * 1000);
}
public void initEvent() {
circleMenu.setOnItemSelectedListener(this);
circleMenu.setOnItemClickListener(this);
img_firefoxt.setOnClickListener(this);
img_ubuntu.setOnClickListener(this);
img_timer.setOnClickListener(this);
}
@Override
public void onItemSelected(View view, int position, long id, String name) {
selectName = name;
}
@Override
public void onItemClick(View view, int position, long id, String name) {
Toast.makeText(this,name,Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.img_firefoxt:
Toast.makeText(this,img_firefoxt.getName(),Toast.LENGTH_SHORT).show();
break;
case R.id.img_timer:
Toast.makeText(this,img_timer.getName(),Toast.LENGTH_SHORT).show();
break;
case R.id.img_ubuntu:
Toast.makeText(this,img_ubuntu.getName(),Toast.LENGTH_SHORT).show();
break;
}
}
}
上面的是Activity的內容但是隻要這些肯定是不夠的還需要一些自定義的控件首先是自定義的中心圓
package demo.liuyongxiang.com.demo.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageView;
import demo.liuyongxiang.com.demo.R;
public class CircleImageView extends ImageView {
private float angle = 0;
private int position = 0;
private String name;
public float getAngle() {
return angle;
}
public void setAngle(float angle) {
this.angle = angle;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public CircleImageView(Context context) {
this(context, null);
}
public CircleImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs,
R.styleable.CircleImageView);
name = a.getString(R.styleable.CircleImageView_name);
}
}
}
還有很多代碼就不在寫出來了,太多了也佔地方還不如直接上demo實惠呢
點擊下載源碼
如有問題請加Android交流羣 470707794或留言