如何在触摸时更改活动背景?

时间:2015-04-15 04:46:30

标签: android android-activity

我在活动中嵌入了一个imageview。当我触摸imageview&时,我需要透明的背景。当我发布imageview时恢复正常。

如果我在setContentView之后或在清单中设置活动主题,我无法在触摸时动态更改它。如何继续?

public class MainActivity extends ActionBarActivity {

   private final String TAG="FrameTest";
   private ImageView ivExpanded,ivCollapsed;
   private FrameLayout frameLayout;
   private float threshold,initialY,finalY,a;
   Animation animFadeIn,animFadeOut;




    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Log.d(TAG,"current theme is"+this.getTheme());
        frameLayout=(FrameLayout)findViewById(R.id.frameLayout);
        ivExpanded=(ImageView)findViewById(R.id.ivExpanded);
        ivCollapsed=(ImageView)findViewById(R.id.ivCollapsed);
        animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.fade_in);
        animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.fade_out);
        //getting threshold
        ivCollapsed.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        threshold=(ivCollapsed.getMeasuredHeight())/6;
        Log.d(TAG, "the threshold is"+threshold);

        ivExpanded.setVisibility(View.GONE);
        //ivCollapsed.setVisibility(View.VISIBLE);

        ivCollapsed.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                int action=event.getAction();

                switch(action)
                {
            case MotionEvent.ACTION_DOWN:
                    initialY=event.getY();
                //  Log.d(TAG,"initialY ="+initialY);
                    break;
            case MotionEvent.ACTION_MOVE:  
             {  
                //downward motion:play animation
                 if(((event.getY()-initialY)>0) && ((event.getY()-initialY)>threshold) )
                 {   Log.d(TAG, "downwrd animation");
                    ivExpanded.setVisibility(View.VISIBLE);
                    ivExpanded.startAnimation(animFadeIn); 
                    ivCollapsed.setVisibility(View.GONE);
                 }
                 //upward motion:animation should not be displayed
                 else if((event.getY()-initialY)<0)
                 {   Log.d(TAG, "no animation");
                     break;
                 }

             }  

            }   
            return true;
            }
        });


        ivExpanded.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                int action=event.getAction();

                switch(action)
                {
            case MotionEvent.ACTION_DOWN:
                    finalY=event.getY();
                //  Log.d(TAG,"initialY ="+initialY);
                    break;
            case MotionEvent.ACTION_MOVE:  
            {   
                //downward motion:animation should not be displayed
                if(event.getY()>finalY)
                {   Log.d(TAG, "no animation");
                    break;
                }   
                //upward motion:less than threshold,no animation
                else if((finalY-event.getY())<threshold)
                {   Log.d(TAG, "no animation");
                    break;
                }   
                else if((finalY-event.getY())>threshold)
                {//show animation
                    Log.d(TAG, "upward animation");

                ivExpanded.setVisibility(View.GONE);
                ivExpanded.startAnimation(animFadeOut);
                ivCollapsed.setVisibility(View.VISIBLE);

                break;
                }
            }   
                }   
                return true;
            }
        });


  }
}

1 个答案:

答案 0 :(得分:0)

按照以下代码段获取活动根视图的触控器:

parentView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                parentView.setBackgroundColor(YourColor);        

                return false;
            }
        });

编辑:过多的谈话让活动变得透明:

https://stackoverflow.com/a/18373009/2624806

https://stackoverflow.com/a/15478675/2624806