If you want the image in the center of the screen, change this:


Y = initialY;


to this:


* Y = (int) (screenH /2) - (arrowH / 2) ;*




On Thursday, September 13, 2012 12:43:56 AM UTC-5, Haris wrote:
>
>
> Hai here is my full code...........
>
> package com.motionsensor;
>
> import java.util.List;
>
> import android.app.Activity;
> import android.content.Context;
> import android.graphics.Bitmap;
> import android.graphics.BitmapFactory;
> import android.graphics.Canvas;
> import android.graphics.Color;
> import android.graphics.Matrix;
> import android.graphics.Paint;
> import android.graphics.Paint.Style;
> import android.hardware.Sensor;
> import android.hardware.SensorEvent;
> import android.hardware.SensorEventListener;
> import android.hardware.SensorManager;
> import android.os.Bundle;
> import android.view.View;
> import android.widget.TextView;
> import android.widget.Toast;
>
> public class MainActivity extends Activity {
>     
>
>  
>  TextView textviewAzimuth, textviewPitch, textviewRoll;
>  private static SensorManager mySensorManager;
>  private boolean sersorrunning;
>  float[] sensorVlaues = new float[3];
>    /** Called when the activity is first created. */
>    @Override
>    public void onCreate(Bundle savedInstanceState) {
>        super.onCreate(savedInstanceState);
>        View v=new MoveArrow(this);
>        setContentView(v);
>        textviewAzimuth = (TextView)findViewById(R.id.textazimuth);
>        textviewPitch = (TextView)findViewById(R.id.textpitch);
>        textviewRoll = (TextView)findViewById(R.id.textroll);
>
>        mySensorManager = 
> (SensorManager)getSystemService(Context.SENSOR_SERVICE);
>        List<Sensor> mySensors = 
> mySensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
>   
>        if(mySensors.size() > 0){
>         mySensorManager.registerListener(mySensorEventListener, 
> mySensors.get(0), SensorManager.SENSOR_DELAY_NORMAL);
>         sersorrunning = true;
>         Toast.makeText(this, "Start ORIENTATION Sensor", 
> Toast.LENGTH_LONG).show();
>        }
>        else{
>         Toast.makeText(this, "No ORIENTATION Sensor", 
> Toast.LENGTH_LONG).show();
>         sersorrunning = false;
>         finish();
>        }
>        
>        
>        
>
>    }
>   
>    private SensorEventListener mySensorEventListener = new 
> SensorEventListener() {
>   
>   @Override
>   public void onSensorChanged(SensorEvent event) {
>        
>           sensorVlaues = event.values.clone();
>
>            
>            
>            MoveArrow.azimuth = String.valueOf(sensorVlaues[0]);
>            MoveArrow.pitch   = String.valueOf(sensorVlaues[1]);
>            MoveArrow.roll    = String.valueOf(sensorVlaues[2]);
>            MoveArrow.angle = -1*(int) sensorVlaues[2];
>
>   }
>   
>   
>   
>   
>   
>   @Override
>   public void onAccuracyChanged(Sensor sensor, int accuracy) {
>    // TODO Auto-generated method stub
>
>   }
>  };
>
>  @Override
>  protected void onDestroy() {
>   // TODO Auto-generated method stub
>   super.onDestroy();
>   
>   if(sersorrunning){
>    mySensorManager.unregisterListener(mySensorEventListener);
>    Toast.makeText(MainActivity.this, "unregisterListener", 
> Toast.LENGTH_SHORT).show();
>   }
>  }
> }
>
>
> class MoveArrow extends View {
>     int screenW;
>     int screenH;
>     static int X;
>     static int Y;
>     int initialY ;
>     int arrowW;
>     int arrowH;
>     int dX_Prev =0;
>     int dY_Prev =0;
>     static int dX_Pres =0;
>     static int dY_Pres =0;
>     static int angle;
>     int dX =0;
>     int dY =0;
>     
>     float acc;
>     Bitmap arrow, bgr;
>     static String azimuth="0";
>     static String pitch="0";
>     static String roll="0";
>     public MoveArrow(Context context) {
>         super(context);
>         arrow = 
> BitmapFactory.decodeResource(getResources(),R.drawable.arrow); //load a 
> arrow image
>         bgr = 
> BitmapFactory.decodeResource(getResources(),R.drawable.graph); //load a 
> background
>         arrowW = arrow.getWidth();
>         arrowH = arrow.getHeight();
>         acc = 0.2f; //acceleration
>         dY = 0; //vertical speed
>         initialY = 100; //Initial vertical position.
>         angle = 0; //Start value for rotation angle.
>     }
>     
>   
>     @Override
>     public void onSizeChanged (int w, int h, int oldw, int oldh) {
>         super.onSizeChanged(w, h, oldw, oldh);
>         screenW = w;
>         screenH = h;
>         bgr = Bitmap.createScaledBitmap(bgr, w, h, true); //Resize 
> background to fit the screen.
>         X = (int) (screenW /2) - (arrowW / 2) ; //Centre arrow into the 
> centre of the screen.
>         Y = initialY;
>     }
>
>     @Override
>     public void onDraw(Canvas canvas) {
>         super.onDraw(canvas);
>         Paint paint = new Paint();
>         paint.setColor(Color.BLACK);
>         paint.setStyle(Style.FILL);
>         paint.setTextSize(12); 
>         paint.setColor(Color.BLUE);
>         //Draw background.
>         canvas.drawBitmap(bgr, 0, 0, null);
>
>        
>
>         int newWidth = arrowW;
>         int newHeight = arrowH;
>         float scaleWidth = 1;
>         float scaleHeight = 1;
>         int centrex = arrowW/2;
>         int centrey = arrowH/2;
>        
>
>         Matrix matrix = new Matrix();
>         matrix.postScale(scaleWidth, scaleHeight);
>         matrix.postRotate(angle, X+centrex , Y+centrey );
>         Bitmap resizedBitmap = Bitmap.createBitmap(arrow, 0, 0, 
> arrow.getWidth(), arrow.getHeight(), matrix, true);
>        
>         
>         canvas.save(Canvas.MATRIX_SAVE_FLAG);
>         canvas.rotate(angle, X + resizedBitmap.getWidth()/2, Y + 
> resizedBitmap.getHeight()/2);
>         super.dispatchDraw(canvas);      
>         canvas.drawBitmap(resizedBitmap, X, Y, null); //Draw the arrow on 
> the rotated canvas.
>         canvas.restore();
>         
>         invalidate();
>     }
> }
>
>
> And I am using an arrow image as my rotating image.....
>
> On Thursday, 13 September 2012 10:44:35 UTC+5:30, Haris wrote:
>>
>> Hai bob thanks for reply.....
>>
>> I made the above change in my code but still the same result....And my 
>> image get distorted it's edges while rotation......
>>
>> On Wednesday, 12 September 2012 20:04:12 UTC+5:30, bob wrote:
>>>
>>> Well, for one thing, this is not right:
>>>
>>> canvas.rotate(angle, X + getWidth() >> 1, Y + getHeight() >> 1);
>>>
>>> You should be calling getWidth and getHeight on the bitmap.
>>>
>>> Something like this:
>>>
>>> canvas.rotate(angle, X + resizedBitmap.getWidth()/2, Y + 
>>> resizedBitmap.getHeight()/2);
>>>
>>>
>>>
>>> On Tuesday, September 11, 2012 12:54:59 AM UTC-5, HPP wrote:
>>>>
>>>> Hai thanks for the replys........
>>>>
>>>> I changed my code like
>>>>
>>>>         matrix.postScale(scaleWidth, scaleHeight);
>>>>         matrix.postRotate(angle, X+centrex , Y+centrey );
>>>>         canvas.setMatrix(matrix);
>>>>
>>>>         Bitmap resizedBitmap = Bitmap.createBitmap(arrow, 0, 0, 
>>>> arrow.getWidth(), arrow.getHeight(), matrix, true);
>>>>         canvas.save(Canvas.MATRIX_SAVE_FLAG);
>>>>         canvas.rotate(angle, *X + *getWidth() >> 1, *Y +* getHeight() 
>>>> >> 1);
>>>>         super.dispatchDraw(canvas);
>>>>         canvas.drawBitmap(resizedBitmap, X, Y, null); //Draw the arrow 
>>>> on the rotated canvas.
>>>>         canvas.restore();
>>>>
>>>> But still it's not rotating about the centre.......
>>>>
>>>>
>>>>
>>>> On Tuesday, 11 September 2012 01:49:30 UTC+5:30, bob wrote:
>>>>>
>>>>> canvas.save(Canvas.MATRIX_SAVE_FLAG);
>>>>> canvas.rotate(angle, *X + *getWidth() >> 1, *Y +* getHeight() >> 1);
>>>>> super.dispatchDraw(canvas);
>>>>> canvas.restore();
>>>>>
>>>>> On Monday, September 10, 2012 2:22:15 PM UTC-5, lbendlin wrote:
>>>>>>
>>>>>>     canvas.save(Canvas.MATRIX_SAVE_FLAG);
>>>>>>     canvas.rotate(angle, getWidth() >> 1, getHeight() >> 1);
>>>>>>     super.dispatchDraw(canvas);
>>>>>>     canvas.restore();
>>>>>>
>>>>>> On Monday, September 10, 2012 9:18:32 AM UTC-4, Haris wrote:
>>>>>>>
>>>>>>> Hai all
>>>>>>>
>>>>>>>  I am trying an application like rotating image with motion 
>>>>>>> sensor....My problem is that the image does not rotate about centre 
>>>>>>> properly....And my angle range is 0 to 90 and 0 to -90..
>>>>>>> And below is my code.....
>>>>>>>
>>>>>>>       public void onDraw(Canvas canvas) {
>>>>>>>         super.onDraw(canvas);
>>>>>>>      Bitmap arrow = 
>>>>>>> BitmapFactory.decodeResource(getResources(),R.drawable.arrow);
>>>>>>>      int arrowW = arrow.getWidth();
>>>>>>>      int arrowH = arrow.getHeight();
>>>>>>>      float scaleWidth = 1;
>>>>>>>      float scaleHeight = 1;
>>>>>>>      int centrex = arrowW/2;
>>>>>>>      int centrey = arrowH/2;
>>>>>>>      int X=108;
>>>>>>>      int Y=100;
>>>>>>>      int angle=0;
>>>>>>>
>>>>>>>        Matrix matrix = new Matrix();
>>>>>>>        matrix.postScale(scaleWidth, scaleHeight);
>>>>>>>
>>>>>>>        matrix.postRotate(angle, X+centrex , Y+centrey );
>>>>>>>        Bitmap resizedBitmap = Bitmap.createBitmap(arrow, 0, 0, 
>>>>>>> arrow.getWidth(), arrow.getHeight(), matrix, true);
>>>>>>>       canvas.save();
>>>>>>>       canvas.drawBitmap(resizedBitmap, X, Y, null);
>>>>>>>       invalidate();
>>>>>>>      }
>>>>>>>
>>>>>>> I am getting the angle values from onCreate method.....Using 
>>>>>>> orientation sensor(roll values)..
>>>>>>> But When I rotate the screen my  image is  rotating but not exactly 
>>>>>>> around the image centre.....
>>>>>>>
>>>>>>> Thanks......
>>>>>>>
>>>>>>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to