caTea 블로그

android frame animation class 본문

android

android frame animation class

ZaRas 2017. 2. 22. 18:28
반응형

/**
* Created by achee7059 on 2017. 2. 22..
*/

public class AnimImageView extends ImageView{

private Context mContext;
private AnimationDrawable mAnimationDrawable;

public AnimImageView(Context context) {
super(context);
mContext= context;
}

public AnimImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
mContext= context;
}

public AnimImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext= context;
}

public boolean initAnimResource(ArrayList<AnimConfig> animImageIdList, boolean isOneShot) {
boolean result = false;

if(animImageIdList != null){
try {
BitmapDrawable bitmapDrawable = null;
mAnimationDrawable = new AnimationDrawable();
mAnimationDrawable.setOneShot(isOneShot); // 반복실행하겠다

for(AnimConfig config : animImageIdList){
bitmapDrawable = (BitmapDrawable)ContextCompat.getDrawable(mContext, config.getImageId());
mAnimationDrawable.addFrame(bitmapDrawable, config.getDuration());
}

this.setBackground(mAnimationDrawable);
result = true;

}catch (Exception e){
result = false;
}
}

return result;
}

public void start(){
if(mAnimationDrawable != null){
mAnimationDrawable.start();
}
}

public void stop(){
if(mAnimationDrawable != null){
mAnimationDrawable.stop();
}
}

public boolean isRunning(){
boolean isRun = false;

if(mAnimationDrawable != null){
isRun = mAnimationDrawable.isRunning();
}

return isRun;
}
}

====================================================================

public class AnimConfig implements Serializable {

/**
* 이미지 리소스 아이디 (Ex. R.drawable.example)
*/
private int imageId;

/**
* 얼마동안 움직일지 시간
*/
private int duration;

public AnimConfig(){}

public AnimConfig(int imageId, int duration){
this.imageId = imageId;
this.duration = duration;
}

public int getImageId() {
return imageId;
}

public void setImageId(int imageId) {
this.imageId = imageId;
}

public int getDuration() {
return duration;
}

public void setDuration(int duration) {
this.duration = duration;
}
}

===================================================

사용방법

mAnimImageView = (AnimImageView) view.findViewById(R.id.recommend_anim_view);

ArrayList<AnimConfig> configList = new ArrayList<AnimConfig>();
AnimConfig config = null;
for (int i = 1; i < 10; i++) {
int resID = getResources().getIdentifier("naruto0"+i, "drawable", "com.ubist.tourtainment.flag");

config = new AnimConfig();
config.setImageId(resID);
config.setDuration(200);
configList.add(config);
}

boolean isSuccess = mAnimImageView.initAnimResource(configList, false);

Log.d(TAG, "anim is Success : "+isSuccess);

mAnimImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mAnimImageView.isRunning()){
mAnimImageView.stop();
mAnimImageView.start();
} else {
mAnimImageView.start();
}

}
});


728x90

'android' 카테고리의 다른 글

AppBarLayout 부드럽게 스크롤  (0) 2017.04.14
Magnify Anim View Pager  (0) 2017.03.13
android permission activity  (0) 2017.02.22
vertical view pager  (0) 2016.07.01
이미지 라운드 처리 클래스  (0) 2016.06.28