2016-07-06 7 views

答えて

1
public class AccelerometerActivity extends Activity implements SensorEventListener { 
private SensorManager sensorManager; 
private Sensor accelerometer; 
private long lastUpdate; 
Bitmap mBitmap1; 

// AnimatedView animatedView = null; 
//ShapeDrawable mDrawable = new ShapeDrawable(); 
public static int x; 
public static int y; 
private ImageView imageView; 
private int bitmapWidth, screenWidth,screenHeight,bitmapHeight; 
int maxxLeft, maxxRight, scrollByX, totalX,totalY,maxTop,maxBottom,scrollByY; 
URL url; 
String filepath = null; 
ProgressDialog progressDialog; 
int b; 

//ProgressDialog pd; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_accelerometer); 

    imageView = (ImageView) findViewById(R.id.imageView1); 
    progressDialog = new ProgressDialog(AccelerometerActivity.this); 
    progressDialog.setMessage("loading"); 

    Display display = getWindowManager().getDefaultDisplay(); 
    Point size = new Point(); 
    display.getSize(size); 
    screenWidth = size.x; 
    screenHeight=size.y; 
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
    accelerometer = sensorManager 
      .getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
    lastUpdate = System.currentTimeMillis();                         


    Resources res = getResources(); 
    Bitmap mBitmap = BitmapFactory.decodeResource(res, R.drawable.panaromic1); 
    BitmapDrawable bDrawable = new BitmapDrawable(res, mBitmap); 
    imageView.setImageBitmap(mBitmap); 

    //get the size of the image and the screen 
    bitmapWidth = bDrawable.getIntrinsicWidth(); 
    //int bw=mBitmap1.getWidth(); 
    bitmapHeight = bDrawable.getIntrinsicHeight(); 

    Log.v("sw",screenWidth+""); 
    Log.v("bw",bitmapWidth+""); 
    //Log.v("sw",screenHeight+""); 
    //Log.v("bw",bitmapHeight+""); 
    int maxX = (int) ((bitmapWidth/2) - (screenWidth/2)); 
    // int maxY = (int) ((bitmapHeight/2) - (screenHeight/2)); 
    maxxLeft = (maxX * -1); 
    maxxRight = maxX; 
    // maxTop = (maxY * -1); 
    //maxBottom = maxY; 
} 






@Override 
protected void onResume() { 
    super.onResume(); 
    sensorManager.registerListener(this, accelerometer, 
      SensorManager.SENSOR_DELAY_GAME); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    sensorManager.unregisterListener(this); 
} 


@Override 
public void onAccuracyChanged(Sensor arg0, int arg1) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onSensorChanged(SensorEvent event) { 
    // TODO Auto-generated method stub 
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { 

     //x -= (int) event.values[0]; 
     //y += (int) event.values[1]; 
     x = (int) event.values[0]; 
     y = (int) event.values[1]; 
     scrollByX = x; 
     scrollByY = y; 
     if (x > 0) { 
      if (totalX == maxxRight) { 
       scrollByX = 0; 
      } 
      if (totalX < maxxRight) { 
       totalX = totalX + scrollByX; 
      } 
      if (totalX > maxxRight) { 
       scrollByX = maxxRight - (totalX - scrollByX); 
       totalX = maxxRight; 
      } 
      /*String xx=""; 
      Log.v("xx:",xx+scrollByX);*/ 
      imageView.scrollBy(scrollByX, 0); 
      /*imageView.scrollBy(5 ,0);*/ 

     } else { 

      if (totalX == maxxLeft) { 
       scrollByX = 0; 
      } 
      if (totalX > maxxLeft) { 
       totalX = totalX + scrollByX; 
      } 
      if (totalX < maxxLeft) { 
       scrollByX = maxxLeft - (totalX - scrollByX); 
       totalX = maxxLeft; 
      } 
      imageView.scrollBy(scrollByX, 0); 

      } 


    } 
} 

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
    ImageView bmImage; 

    public DownloadImageTask(ImageView bmImage) { 
     this.bmImage = bmImage; 
    } 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 
     progressDialog.show(); 

     //pd.show(); 
    } 

    protected Bitmap doInBackground(String... urls) { 
     String urldisplay = urls[0]; 
     Bitmap mIcon11 = null; 
     try { 
      InputStream in = new java.net.URL(urldisplay).openStream(); 
      mBitmap1 = BitmapFactory.decodeStream(in); 
      //b=mBitmap1.getWidth(); 

     } catch (Exception e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 
     return mBitmap1; 
    } 

    @Override 
    protected void onPostExecute(Bitmap result) { 
     super.onPostExecute(result); 
     //pd.dismiss(); 

     progressDialog.dismiss(); 

    } 
} 
} 
+0

そのはme.Iのために働いていないが、同じコードを置くが、何も起こりません。 –

+0

取得しているエラーは何ですか? –

+0

私はそのエラーを解決しました。私の質問は、あなたのコードでは画像を移動するだけなので、画像を変更する方法ですが、電話機を右から左に動かすと画像のシーケンスを使用して移動します。 それはどのようにすることが可能ですか?私はあなたの答えに投票しました。 –

関連する問題