单击视图时为什么没有动画?

时间:2016-11-18 04:45:54

标签: android android-animation

我正在使用此代码:

package com.example.tileanimator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

        GridView gridview = (GridView) findViewById(R.id.gridview);
        final ImageAdapter ia = new ImageAdapter(this);
        gridview.setAdapter( ia );

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {

                        for( int i = 0; i < ia.getCount(); i++ ) {
                            if( position != i ) {
                                View toFade = ia.getView(i, null, parent);
                                fadeout( toFade );
                                    Toast.makeText(MainActivity.this, "Fading out: " + i,
                                        Toast.LENGTH_SHORT).show();
                            }
                        }
            }
        });
    }

    private void fadeout( View v ) {;

        v.setAlpha(1f);
        v.setVisibility(View.VISIBLE);

        v.animate()
                .alpha(0f)
                .setDuration(1000);

    }
}

基本上,这里的例子是:https://developer.android.com/guide/topics/ui/layout/gridview.html

当我点击任何图像时,我看到Toast,但没有显示动画。知道为什么吗?

(我也很困惑,是否将null作为第二个参数传递给getView()是一个坏主意,如果是,那么传递什么。)

如果重要,我的ImageAdapter就在这里:

package com.example.tileanimator;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

/**
 * Created by xrdawson on 11/17/16.
 */

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };
}

3 个答案:

答案 0 :(得分:0)

  • 首先在你的淡化方法中移动你的吐司。确认方法Fade是否有效。

  • 其次,我注意到你的fadeout方法的第一个支撑后面有一个不必要的分号。删除它

    p ....(查看v){; &LT; ----------

  • 第三,您是否在v上应用了alpha两次?

    v.setAlpha(1f);
    v.animate()
            .alpha(0f)
    

删除最初的那个。一个就足够了。

  • 第四个在.setDuration之后立即使clicklistener无效,同样地:

    private void fadeout( View v ) {;
      v.setAlpha(1f);
      v.setVisibility(View.VISIBLE);
    
      v.animate()
            .alpha(0f)
            .setDuration(1000)
            .setListener(null);
    
  • 最后,如果之前的所有内容都没有帮助,那么继续将网格视图带到fadeout方法并使用switch语句

    private void fadeout( View v ) {
    
      switch(v.getId()){
    
    
         case R.id.gridview:
    
    
    
    
    gridview.setVisibility(View.VISIBLE);
    gridview.animate()
            .alpha(0f)
            .setDuration(1000)
            .setListener(null);
    
    
    
      break;
    
     }
    

答案 1 :(得分:0)

问题是我实际上并没有在应用程序中检索视图。通过使用null作为第二个参数调用View toFade = ia.getView(i, null, parent);,我生成了一个未附加到视图层次结构的新视图。获取动画视图的正确代码应该是这样的:

View toFade = gridview.getChildAt( i );

if( toFade != null && v != toFade ) {
  fadeout( toFade, i );
}

答案 2 :(得分:-1)

尝试使用

fade(v)

代码,看它是否有效。