2 qq 18804935 qq_18804935 于 2014.08.14 10:01 提问

android游戏同时实现图片自动循环移动和另一张图片随手指移动二移动

package com.example.text;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.text.R;
/**u

  • MainActivity
  • @author xinye
  • */
    public class MainActivity extends BaseActivity implements OnTouchListener{
    private Handler mHandler = new Handler();
    private ImageView mImageView;
    private View mView;
    @Override
    protected void inflateContentView() {
    mView = layoutInflater.inflate(R.layout.activity_main, null);
    mImageView = (ImageView) mView.findViewById(R.id.imageView);
    contentView.addView(mView, contentViewDefaultParams);
    mView.measure(50, 50);
    mImageView.measure(50, 50);
    new Thread(new Runnable() {
    @Override
    public void run() {
    mHandler.post(new Runnable() {
    @Override
    public void run() {
    TranslateAnimation ta1 = new TranslateAnimation(
    TranslateAnimation.RELATIVE_TO_PARENT, 0,
    TranslateAnimation.RELATIVE_TO_PARENT, 0 ,
    TranslateAnimation.RELATIVE_TO_PARENT, 0,
    TranslateAnimation.RELATIVE_TO_PARENT,
    1 - (mImageView.getMeasuredHeight()) * 1.0f / mView.getMeasuredHeight());
    ta1.setDuration(2000);
    ta1.setStartTime(0);
    ta1.setRepeatCount(Integer.MAX_VALUE);
    ta1.setRepeatMode(Animation.REVERSE);
    mImageView.startAnimation(ta1);
    }
    });
    }
    }).start();

    }
    }

    public class MainActivity_one extends Activity implements OnTouchListener {
    int screenWidth;
    int screenHeight;

    int iv_Id[]={R.id.iv1};
    int drawable_Id[]={R.drawable.ooo};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    DisplayMetrics dm = getResources().getDisplayMetrics();
    screenWidth= dm.widthPixels;
    screenHeight= dm.heightPixels - 50;
    
     for(int i=0;i<iv_Id.length;i++){
    
         ImageView iv=(ImageView)this.findViewById(iv_Id[i]);
         iv.setImageResource(drawable_Id[i]);
         iv.setOnTouchListener((OnTouchListener) this);
     }
    

    // b.setOnTouchListener(this);
    }
    int lastX,lastY;
    public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    int ea=event.getAction();
    Log.i("TAG", "Touch:"+ea);

    //Toast.makeText(DraftTest.this, "位置:"+x+","+y, Toast.LENGTH_SHORT).show();
    
    switch(ea){
    case MotionEvent.ACTION_DOWN:
        lastX = (int) event.getRawX();
        lastY = (int) event.getRawY();
        break;
        /**
         * layout(l,t,r,b)
         * l  Left position, relative to parent 
          t  Top position, relative to parent 
          r  Right position, relative to parent 
          b  Bottom position, relative to parent  
         * */
    case MotionEvent.ACTION_MOVE:
        int dx =(int)event.getRawX() - lastX;
        int dy =(int)event.getRawY() - lastY;
    
        int left = v.getLeft() + dx;
        int top = v.getTop() + dy;
        int right = v.getRight() + dx;
        int bottom = v.getBottom() + dy;
    
        if(left < 0){
            left = 0;
            right = left + v.getWidth();
        }
    
        if(right > screenWidth){
            right = screenWidth;
            left = right - v.getWidth();
        }
    
        if(top < 0){
            top = 0;
            bottom = top + v.getHeight();
        }
    
        if(bottom > screenHeight){
            bottom = screenHeight;
            top = bottom - v.getHeight();
        }
    
        v.layout(left, top, right, bottom);
    
        Log.i("", "position:" + left +", " + top + ", " + right + ", " + bottom);
    
        lastX = (int) event.getRawX();
        lastY = (int) event.getRawY();
    
        break;
    case MotionEvent.ACTION_UP:
        break;              
    }
    return true;
    

    }
    }

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!