3

I am beginner in android programming.I want to write a program that should to change position of circle with accelerometer sensor. my code is in the below, the position of circle was changed but the circle not moved to right or down of screen, the circle moved a little :( even if i moved too much the mobile phone . My question : How can i moving a ball(circle) to below or right of screen with accelerometer sensor?

public class MainActivity extends Activity implements SensorEventListener{

ShapeDrawable shapeDrawable=new ShapeDrawable();
public static int x;
public static int y;
public static Object bimtap;
Bitmap bitmap;

private SensorManager sm;

CustomDrawableView cDraw;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    sm=(SensorManager)getSystemService(SENSOR_SERVICE);
    cDraw=new CustomDrawableView(this);
    setContentView(cDraw);
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

}

public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
        //
        x=(int)Math.pow(event.values[0], 2);
        y=(int)Math.pow(event.values[1], 2);
    }
}

@Override
public void onResume(){
    super.onResume();
    sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
public void onStop(){
    sm.unregisterListener(this);
    super.onStop();
}

public class CustomDrawableView extends View{

    int width=150;
    int height=150;

    public CustomDrawableView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        shapeDrawable=new ShapeDrawable();
    }

    public void onDraw(Canvas canvas){
        Paint paint=new Paint();
        paint.setColor(Color.BLUE);
        canvas.drawCircle(MainActivity.x+50, MainActivity.y+50, 25, paint);
        invalidate();

    }

}
}

Thanks for help :) regard

1 Answer 1

0

Following code is move the circle where ever you click

public class Custom_View extends View {
private PointF point;
private Paint paint;

public Custom_View(Context context) {
    this(context, null);
}

public Custom_View(Context context, AttributeSet attrs) {
    super(context, attrs);
    paint = new Paint(Paint.ANTI_ALIAS_FLAG);
}

@Override
protected void onDraw(Canvas canvas) {
    if (point != null) {
        int radius = 50;
            paint.setColor(Color.GREEN);
            canvas.drawCircle(point.x, point.y, radius, paint);
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        point = new PointF(event.getX(), event.getY());
        break;
    case MotionEvent.ACTION_UP:
        point = null;
        break;
    case MotionEvent.ACTION_MOVE:
        point = new PointF(event.getX(), event.getY());
        break;
    }
    invalidate();
    return true;
}
5
  • Thanks. but i want, the circle moved in all of the screen with value of accelerometer not with my click in screen.
    – Sensors
    Jul 10, 2013 at 4:15
  • Not even close to answering the question.
    – async
    Mar 28, 2014 at 20:46
  • @user16547 then how he accept the answer? Do you know? then answer it don't put downvotes. I mentioned in my first line of answer..
    – Aravin
    Mar 29, 2014 at 3:06
  • @user16547 read the question and answer contents fully not just title.
    – Aravin
    Mar 29, 2014 at 3:12
  • "when how he accept the answer?" I don't know and I don't care. I'm interested in the answer and all I'm seeing is an attempt to answer a different question. And it seems that the OP noticed the same thing in his comment.
    – async
    Mar 29, 2014 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.