Help us understand the problem. What is going on with this article?

ListViewをスクロール時に、透明度を操作してふんわり表示してみる

More than 5 years have passed since last update.

やってみたこと

ArrayAdapterのgetViewメソッドでObjectAnimatorクラスを使用し、アルファ値を500ミリ秒かけて透明から非透明にしてみる

ObjectAnimatorとは?

簡単にViewにアニメーションを付けられる

ObjectAnimator objectAnimator = ObjectAnimator.ofFloat( target, "alpha", 0f, 1f );
objectAnimator.setDuration( 500 );
objectAnimator.start();

target が Viewのオブジェクトです。動かしたいviewを渡します。
参考(というかほぼそのまま):http://techblog.yahoo.co.jp/programming/androidiphone/

ListViewのbind時に使ってみた

import java.util.ArrayList;
import java.util.List;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<String> list = new ArrayList<String>();
        for(int i=0;i<=100;i++){ list.add(i+""); }
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(new CustomAdapter(this.getApplicationContext(), 0, list));
    }

    private class CustomAdapter extends ArrayAdapter<String>{
        private LayoutInflater mInflater;
        private int mLastPosition = -1;

        public CustomAdapter(Context context, int resourceId, List<String> objects) {
            super(context, resourceId, objects);
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (null == convertView) {
                convertView = mInflater.inflate(R.layout.custom_view, null);
            }
            String item = (String)getItem(position);
            TextView text = (TextView)convertView.findViewById(R.id.text_view);
            text.setText(item);

            // ここの判定をいれなければ上に行った時もアニメーションする
            if(mLastPosition < position){
                // ここでアニメーションを設定する
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat( text, "alpha", 0f, 1f );
                objectAnimator.setDuration( 500 );
                objectAnimator.start();
                mLastPosition = position;
            }
            return convertView;
        }
    }
}
ueno-yuhei
水樹奈々が好き!
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away