Reputation: 9
I'm having problems making an ImageView (dbm_needle) that I would like to rotate using the code below after a button push, display correctly on the RelativeLayout. The content appears to be drawn correctly to start, but the button push does not seem to initiate the onTouch event that makes the needle rotate and I can't seem to figure out what is wrong.
counter.xml:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/dblmeter_screen"
android:gravity="center_horizontal">
Java Activity:
public class Counter extends Activity {
private boolean keepRotating = false;
private boolean returnRotating = true;
private Random mRnd = new Random();
private static final int INTERVAL = 25;
int degrees =-65;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.counter);
}
public void make(float x){
RelativeLayout relLayout = (RelativeLayout) findViewById(R.id.dblmeter_screen) ;
Bitmap rotate_needle = BitmapFactory.decodeResource(getResources(), R.drawable.dbm_needle);
int width = rotate_needle.getWidth();
int height = rotate_needle.getHeight();
int newWidth = 6;
int newHeight = 400;
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
matrix.postRotate(x);
Bitmap resizedBitmap = Bitmap.createBitmap(rotate_needle, 0, 0,
width, height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
ImageView imageView = new ImageView(this);
imageView.setImageDrawable(bmd);
imageView.setScaleType(ScaleType.CENTER);
setContentView(relLayout);
}
public void make(int degrees)
{
Log.i(this.getClass().toString(), "Rotation : " + degrees);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
startRotating();
break;
case MotionEvent.ACTION_UP:
stopRotating();
break;
}
return super.onTouchEvent(event);
}
public void startRotating()
{
returnRotating = false;
if (!keepRotating)
{
keepRotating = true;
final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
if (keepRotating)
{
int Rand = mRnd.nextInt(30);
degrees = (degrees + (Rand/2)-Rand/6) % 360;
if(degrees > 65)
degrees = degrees - Rand;
make(degrees);
handler.postDelayed(this, INTERVAL);
}
}
}, INTERVAL);
}
}
public void stopRotating()
{
keepRotating = false;
if (!returnRotating)
{
returnRotating = true;
final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
if (returnRotating)
{
int Rand = mRnd.nextInt(30);
degrees = (degrees - (Rand/2)+Rand/6) % 360;
if(degrees < -65)
degrees = degrees + Rand;
make(degrees);
handler.postDelayed(this, INTERVAL);
}
}
}, INTERVAL);
}
}
public void pushClick(View pushClick) {
switch (pushClick.getId()) {
case R.id.btn_push:
make(degrees);
}
}
Upvotes: 0
Views: 750
Reputation: 31779
The stack trace seems to show an internal error. Sometimes eclipse can screw things up for no reason. Try cleaning the project or restarting eclipse or restarting adb. That might fix your issue.
Then I think you should add layout parameters to the imageview created like
imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT
,LayoutParams.WRAP_CONTENT));
Upvotes: 1