PDA

View Full Version : باز کردن یک قطعه کد



gilas1368
سه شنبه 20 آبان 1393, 19:01 عصر
سلام ب همه

کسی از دوستان میتونه کد زیرو برام تشریح کنه؟



public class MyActivity extends ActionBarActivity {
private static Bitmap imageOriginal, imageScaled;
private static Matrix matrix;


private ImageView dialer;
private int dialerHeight, dialerWidth;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);


// load the image only once
if (imageOriginal == null) {
imageOriginal = BitmapFactory.decodeResource(getResources(), R.drawable.jog);
}


// initialize the matrix only once
if (matrix == null) {
matrix = new Matrix();
} else {
// not needed, you can also post the matrix immediately to restore the old state
matrix.reset();
}




dialer = (ImageView) findViewById(R.id.imgKnob);
//dialer.setOnTouchListener(new MyOnTouchListener());
dialer.getViewTreeObserver().addOnGlobalLayoutList ener(new ViewTreeObserver.OnGlobalLayoutListener() {


@Override
public void onGlobalLayout() {
// method called more than once, but the values only need to be initialized one time
if (dialerHeight == 0 || dialerWidth == 0) {
dialerHeight = dialer.getHeight();
dialerWidth = dialer.getWidth();


// resize
Matrix resize = new Matrix();
resize.postScale((float)Math.min(dialerWidth, dialerHeight) / (float)imageOriginal.getWidth(), (float)Math.min(dialerWidth, dialerHeight) / (float)imageOriginal.getHeight());
imageScaled = Bitmap.createBitmap(imageOriginal, 0, 0, imageOriginal.getWidth(), imageOriginal.getHeight(), resize, false);
}
}
});
}




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


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}


private class MyOnTouchListener implements View.OnTouchListener {


private double startAngle;


@Override
public boolean onTouch(View v, MotionEvent event) {


switch (event.getAction()) {


case MotionEvent.ACTION_DOWN:
startAngle = getAngle(event.getX(), event.getY());
break;


case MotionEvent.ACTION_MOVE:
double currentAngle = getAngle(event.getX(), event.getY());
rotateDialer((float) (startAngle - currentAngle));
startAngle = currentAngle;
break;


case MotionEvent.ACTION_UP:


break;
}


return true;
}


}


private double getAngle(double xTouch, double yTouch) {
double x = xTouch - (dialerWidth / 2d);
double y = dialerHeight - yTouch - (dialerHeight / 2d);


switch (getQuadrant(x, y)) {
case 1:
return Math.asin(y / Math.hypot(x, y)) * 180 / Math.PI;
case 2:
return 180 - Math.asin(y / Math.hypot(x, y)) * 180 / Math.PI;
case 3:
return 180 + (-1 * Math.asin(y / Math.hypot(x, y)) * 180 / Math.PI);
case 4:
return 360 + Math.asin(y / Math.hypot(x, y)) * 180 / Math.PI;
default:
return 0;
}
}


/**
* @return The selected quadrant.
*/
private static int getQuadrant(double x, double y) {
if (x >= 0) {
return y >= 0 ? 1 : 4;
} else {
return y >= 0 ? 2 : 3;
}
}


private void rotateDialer(float degrees) {
matrix.postRotate(degrees);
dialer.setImageBitmap(Bitmap.createBitmap(imageSca led, 0, 0, imageScaled.getWidth(), imageScaled.getHeight(), matrix, true));
}


public void btnTest(View view){
switch (view.getId()){
case R.id.btnDown:
rotateDialer(-35);
break;
case R.id.btnUp:
rotateDialer(35);
break;
}
}
}