【问题标题】:Android TextView Animation issueAndroid TextView 动画问题
【发布时间】:2013-11-07 09:33:26
【问题描述】:

我需要使用文本 Dtextview 制作动画。但是当我运行程序时,它会为带有文本 Atextview 和带有文本 Dtextview 设置动画。带有文本 Atextview 是列表中的第一个textview

public class CustomArrayAdapter extends ArrayAdapter<String> {

private Context context;
private int layoutResourceId;
private List<String> data = null;

public CustomArrayAdapter(Context cont, int textViewResourceId,
        List<String> horaNames) {
    super(cont, textViewResourceId, horaNames);

    this.layoutResourceId = textViewResourceId;
    this.context = cont;
    this.data = horaNames;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;
    ItemHolder holder = null;
    if (row == null) {
        LayoutInflater inflater = LayoutInflater.from(context);

        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ItemHolder();
        holder.itemTV = (TextView) row.findViewById(R.id.listItemTV);

        row.setTag(holder);
    } else {
        holder = (ItemHolder) row.getTag();
    }

    // set list view background color
    row.setBackgroundColor(Color.DKGRAY);

    holder.itemTV.setText(data.get(position));

    Animation textAnimation = null;

    if (data.get(position).equals("A")) {
        holder.itemTV.setTextColor(Color.CYAN);
    } else if (data.get(position).equals("B")) {
        holder.itemTV.setTextColor(Color.GREEN);
    } else if (data.get(position).equals("C")) {
        holder.itemTV.setTextColor(Color.MAGENTA);
    } else if (data.get(position).equals("D")) {
        holder.itemTV.setTextColor(Color.RED);
        textAnimation = new AlphaAnimation(0.0f, 1.0f);
        textAnimation.setDuration(800);
        textAnimation.setStartOffset(20);
        textAnimation.setRepeatMode(Animation.REVERSE);
        textAnimation.setRepeatCount(Animation.INFINITE);
        holder.itemTV.startAnimation(textAnimation);
    } else if (data.get(position).equals("E")) {
        holder.itemTV.setTextColor(Color.WHITE);
    } else if (data.get(position).equals("F")) {
        holder.itemTV.setTextColor(Color.YELLOW);
    } else if (data.get(position).equals("G")) {
        holder.itemTV.setTextColor(Color.BLUE);
    }
    textAnimation = null;
    return row;
}

static class ItemHolder {
    TextView itemTV;
}

我该如何解决这个问题?我只需要使用文本 Dtextview 设置动画。

更新

ma​​in_list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" >

    <TextView
        android:id="@+id/listItemTV"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:textSize="20sp"
        android:textStyle="bold" />

</RelativeLayout>

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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp" />

</RelativeLayout>

Complete project download.

Link 2

【问题讨论】:

标签: android animation android-listview textview android-4.4-kitkat


【解决方案1】:

像这样改变你的 getView() 方法然后它就可以工作了..

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;
    ItemHolder holder = null;
    if (row == null) {
        LayoutInflater inflater = LayoutInflater.from(context);

        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ItemHolder();
        holder.itemTV = (TextView) row.findViewById(R.id.listItemTV);

        row.setTag(holder);
    } else {
        holder = (ItemHolder) row.getTag();
    }

    // set list view background color
    row.setBackgroundColor(Color.DKGRAY);

    holder.itemTV.setText(data.get(position));

    Animation textAnimation = null;

    if (data.get(position).equals("A")) {
        holder.itemTV.setTextColor(Color.CYAN);
    } else if (data.get(position).equals("B")) {
        holder.itemTV.setTextColor(Color.GREEN);
    } else if (data.get(position).equals("C")) {
        holder.itemTV.setTextColor(Color.MAGENTA);
    } else if (data.get(position).equals("D")) {
        holder.itemTV.setTextColor(Color.RED);
    } else if (data.get(position).equals("E")) {
        holder.itemTV.setTextColor(Color.WHITE);
    } else if (data.get(position).equals("F")) {
        holder.itemTV.setTextColor(Color.YELLOW);
    } else if (data.get(position).equals("G")) {
        holder.itemTV.setTextColor(Color.BLUE);
    }

    if (holder.itemTV.getText().equals("D")) {
        textAnimation = new AlphaAnimation(0.0f, 1.0f);
        textAnimation.setDuration(800);
        textAnimation.setStartOffset(20);
        textAnimation.setRepeatMode(Animation.REVERSE);
        textAnimation.setRepeatCount(Animation.INFINITE);
        holder.itemTV.setAnimation(textAnimation);
    } else if (holder.itemTV.getText().equals("A")) {
        holder.itemTV.setAnimation(null);
    }

    textAnimation = null;
    return row;
}

这也是可行的..我认为这个更好..

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;
    ItemHolder holder = null;
    if (row == null) {
        LayoutInflater inflater = LayoutInflater.from(context);

        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ItemHolder();
        holder.itemTV = (TextView) row.findViewById(R.id.listItemTV);

        row.setTag(holder);
    } else {
        holder = (ItemHolder) row.getTag();
    }

    // set list view background color
    row.setBackgroundColor(Color.DKGRAY);

    holder.itemTV.setText(data.get(position));

    Animation textAnimation = null;

    if (data.get(position).equals("A")) {
        holder.itemTV.setTextColor(Color.CYAN);
        holder.itemTV.setAnimation(null);
    } else if (data.get(position).equals("B")) {
        holder.itemTV.setTextColor(Color.GREEN);
    } else if (data.get(position).equals("C")) {
        holder.itemTV.setTextColor(Color.MAGENTA);
    } else if (data.get(position).equals("D")) {
        holder.itemTV.setTextColor(Color.RED);
        textAnimation = new AlphaAnimation(0.0f, 1.0f);
        textAnimation.setDuration(800);
        textAnimation.setStartOffset(20);
        textAnimation.setRepeatMode(Animation.REVERSE);
        textAnimation.setRepeatCount(Animation.INFINITE);
        holder.itemTV.setAnimation(textAnimation);
    } else if (data.get(position).equals("E")) {
        holder.itemTV.setTextColor(Color.WHITE);
    } else if (data.get(position).equals("F")) {
        holder.itemTV.setTextColor(Color.YELLOW);
    } else if (data.get(position).equals("G")) {
        holder.itemTV.setTextColor(Color.BLUE);
    }

    textAnimation = null;
    return row;
}

【讨论】:

    【解决方案2】:

    Ofcource kalyan 解决了您的问题,但他不知道您是如何解决该问题的,所以我解决了。其实删除这行代码

    if (row == null) {
    

    } else {
        holder = (ItemHolder) row.getTag();
    }
    

    现在你的 getView 方法是:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        View row = convertView;
        ItemHolder holder = null;
    
        LayoutInflater inflater = LayoutInflater.from(context);
    
        row = inflater.inflate(layoutResourceId, parent, false);
    
        holder = new ItemHolder();
        holder.itemTV = (TextView) row.findViewById(R.id.listItemTV);
    
        row.setTag(holder);
    
        // set list view background color
        row.setBackgroundColor(Color.DKGRAY);
    
        holder.itemTV.setText(data.get(position));
    
        Animation textAnimation = null;
    
        if (data.get(position).equals("A")) {
            holder.itemTV.setTextColor(Color.CYAN);
        } else if (data.get(position).equals("B")) {
            holder.itemTV.setTextColor(Color.GREEN);
        } else if (data.get(position).equals("C")) {
            holder.itemTV.setTextColor(Color.MAGENTA);
        } else if (data.get(position).equals("D")) {
            holder.itemTV.setTextColor(Color.RED);
            textAnimation = new AlphaAnimation(0.0f, 1.0f);
            textAnimation.setDuration(800);
            textAnimation.setStartOffset(20);
            textAnimation.setRepeatMode(Animation.REVERSE);
            textAnimation.setRepeatCount(Animation.INFINITE);
            holder.itemTV.startAnimation(textAnimation);
            textAnimation = null;
        } else if (data.get(position).equals("E")) {
            holder.itemTV.setTextColor(Color.WHITE);
        } else if (data.get(position).equals("F")) {
            holder.itemTV.setTextColor(Color.YELLOW);
        } else if (data.get(position).equals("G")) {
            holder.itemTV.setTextColor(Color.BLUE);
        }
        textAnimation = null;
        return row;
    }
    

    【讨论】:

    • 谢谢阿南德。非常感谢您的帮助:)
    【解决方案3】:

    我是用这段代码完成的

    我的主要课程

    public class ListClass extends Activity {
    ListView listView;
    List<String> item = new ArrayList<String>();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listclass);
    
        item.add("A");
        item.add("B");
        item.add("C");
        item.add("D");
        item.add("E");
        item.add("F");
        item.add("G");
    
        listView = (ListView) findViewById(R.id.listView);
        CustomArrayAdapter adapter = new CustomArrayAdapter(
                getApplicationContext(), R.layout.listitem, item);
        listView.setAdapter(adapter);
    
    }
    

    }

    适配器

    public class CustomArrayAdapter extends ArrayAdapter<String> {
    
    private Context context;
    private int layoutResourceId;
    private List<String> data = null;
    
    public CustomArrayAdapter(Context cont, int textViewResourceId,
            List<String> horaNames) {
        super(cont, textViewResourceId, horaNames);
    
        this.layoutResourceId = textViewResourceId;
        this.context = cont;
        this.data = horaNames;
    
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        View row = convertView;
        ItemHolder holder = null;
        if (row == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
    
            row = inflater.inflate(layoutResourceId, parent, false);
    
            holder = new ItemHolder();
            holder.itemTV = (TextView) row.findViewById(R.id.textView1);
    
            row.setTag(holder);
        } else {
            holder = (ItemHolder) row.getTag();
        }
    
        // set list view background color
        row.setBackgroundColor(Color.DKGRAY);
    
        holder.itemTV.setText(data.get(position));
    
        Animation textAnimation = null;
    
        if (data.get(position).equals("A")) {
            holder.itemTV.setTextColor(Color.CYAN);
        } else if (data.get(position).equals("B")) {
            holder.itemTV.setTextColor(Color.GREEN);
        } else if (data.get(position).equals("C")) {
            holder.itemTV.setTextColor(Color.MAGENTA);
        } else if (data.get(position).equals("D")) {
            holder.itemTV.setTextColor(Color.RED);
            textAnimation = new AlphaAnimation(0.0f, 1.0f);
            textAnimation.setDuration(800);
            textAnimation.setStartOffset(20);
            textAnimation.setRepeatMode(Animation.REVERSE);
            textAnimation.setRepeatCount(Animation.INFINITE);
            holder.itemTV.startAnimation(textAnimation);
        } else if (data.get(position).equals("E")) {
            holder.itemTV.setTextColor(Color.WHITE);
        } else if (data.get(position).equals("F")) {
            holder.itemTV.setTextColor(Color.YELLOW);
        } else if (data.get(position).equals("G")) {
            holder.itemTV.setTextColor(Color.BLUE);
        }
        textAnimation = null;
        return row;
    }
    
    static class ItemHolder {
        TextView itemTV;
    }
    

    }

    和你的一样

    和xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:textSize="20sp"
        android:textStyle="bold" />
    

    第二个xml

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

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>
    

    成功了

    【讨论】:

      猜你喜欢
      • 2014-01-31
      • 1970-01-01
      • 2011-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-22
      • 2011-04-17
      相关资源
      最近更新 更多