集成拖放和旋转

时间:2013-11-03 07:14:51

标签: android

我在android中创建了两个不同的项目,用于旋转和拖放。那些工作正常。但是,我需要将它们整合到一个项目中。请帮忙,

提前致谢。

以下显示的MainActivity代码均为:

//这是拖放MainActivity.java代码 package com.example.draganddrop;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

public class MainActivity extends Activity implements OnTouchListener
{
    MySurfaceView MSV;
    float x, y;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    MSV = new MySurfaceView(this);
    MSV.setOnTouchListener(this);
    x = 0;
    y = 0;
    setContentView(MSV);
}

protected void onPause()
{
    super.onPause();
    MSV.pause();
}

protected void onResume()
{
    super.onResume();
    MSV.resume();
}

@Override
public boolean onTouch(View v, MotionEvent event) 
{
    x = event.getX();
    y = event.getY();
    return true;
}

public class MySurfaceView extends SurfaceView implements Runnable
{
    SurfaceHolder ourHolder;
    Thread ourThread = null;
    boolean isRunning = false;

    public MySurfaceView(Context context) 
    {
        super(context);
        ourHolder = getHolder();

    }

    public void pause()
    {
        isRunning = false;
        while(true)
        {
            try 
            {
                ourThread.join();
            } 
            catch (InterruptedException e) 
            {
                e.printStackTrace();
            }
            break;
        }
        ourThread = null;
    }

    public void resume()
    {
        isRunning = true;    
        ourThread = new Thread(this);
        ourThread.start();
    }

    @Override
    public void run() 
    {
        while(isRunning)
        {
            if(!ourHolder.getSurface().isValid())
                continue;
            Canvas canvas = ourHolder.lockCanvas();
            canvas.drawRGB(12, 12, 150);
            if(x != 0 && y != 0)
            {
                Bitmap test = BitmapFactory.decodeResource(getResources(), R.drawable.colorball);
                canvas.drawBitmap(test, x-(test.getWidth()/2), y-(test.getHeight()/2), null);
            }
            ourHolder.unlockCanvasAndPost(canvas);
        }
    }

}

}


//这是旋转MainActivity.java代码 package com.example.rotatetest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnTouchListener {

private ImageView mCircle;
private double mCurrAngle = 0;
private double mPrevAngle = 0;

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

    mCircle = (ImageView) findViewById(R.id.imageView1);
    mCircle.setOnTouchListener( this); // Your activity should implement OnTouchListener
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    final float xc = mCircle.getWidth() / 2;
    final float yc = mCircle.getHeight() / 2;

    final float x = event.getX();
    final float y = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN: {
        mCircle.clearAnimation();
        mCurrAngle = Math.toDegrees(Math.atan2(x - xc, yc - y));
        break;
    }
    case MotionEvent.ACTION_MOVE: {
        mPrevAngle = mCurrAngle;
        mCurrAngle = Math.toDegrees(Math.atan2(x - xc, yc - y));
        animate(mPrevAngle, mCurrAngle, 0);
        break;
    }
    case MotionEvent.ACTION_UP : {
        mPrevAngle = mCurrAngle = 0;
        break;
    }
    }

    return true;
}

private void animate(double fromDegrees, double toDegrees, long durationMillis) {
    final RotateAnimation rotate = new RotateAnimation((float) fromDegrees, (float) toDegrees,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f);
    rotate.setDuration(durationMillis);
    rotate.setFillEnabled(true);
    rotate.setFillAfter(true);
    mCircle.startAnimation(rotate);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

0 个答案:

没有答案