我想在屏幕上显示3个项目:SeekBar,背景img,可旋转img。
任务:绘制2张图像:量规和一根针,然后根据搜索杆的进度旋转针。
当我尝试上传到平板电脑时出现问题
07-10 09:52:13.680: E/AndroidRuntime(30494): java.lang.RuntimeException:
Unable to start activity ComponentInfo{com.aaa.samples.thermometer/com.aaa.samples.meter.MeterActivity}:
java.lang.ClassCastException: com.aaa.samples.meter.MeterActivity cannot be cast to android.widget.SeekBar$OnSeekBarChangeListener
尝试了很多不同的设置,包括relativelayout等。没有运气
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="150" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/spidometras" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/rodykle" />
</FrameLayout>
如有需要的课程:
seekBar = (SeekBar)findViewById(R.id.seekBar1);
seekBar.setOnSeekBarChangeListener((OnSeekBarChangeListener) this);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
ImageView img = (ImageView)findViewById(R.id.imageView1);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.rodykle);
// Getting width & height of the given image.
int w = bmp.getWidth();
int h = bmp.getHeight();
// Setting post rotate to 90
Matrix mtx = new Matrix();
mtx.postRotate(progress);
// Rotating Bitmap
Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);
img.setImageDrawable(bmd);
}
});
}
最佳答案
删除seekBar.setOnSeekBarChangeListener((OnSeekBarChangeListener) this);
行
在类代码中仔细查看
下一行:
seekBar.setOnSeekBarChangeListener((OnSeekBarChangeListener) this);
尝试在
OnSeekBarChangeListener
中键入case活动,并在下一行附加新的OnSeekBarChangeListener
。因此,只需删除此行。