简体   繁体   English

旋转时绘制多个位图

[英]multiple bitmaps drawn while rotating

I have an activity that rotates png image by the SeekBar progress. 我有一个通过SeekBar进度旋转png图像的活动。 And it draws me multiple needles. 它吸引了我多根针。 why it does that? 为什么这样做呢? The needle and the speedometer are not perfect, they are just for testings :) 指针和车速表并不完美,它们仅用于测试:)

In onCreate: 在onCreate中:

@Override
public void onCreate(Bundle savedInstanceState) {

... ...

    iv = (ImageView)findViewById(R.id.imageView1);
    bMap = BitmapFactory.decodeResource(getResources(), R.drawable.rodykle);
    workingBitmap = Bitmap.createBitmap(bMap);
    mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
    yourCanvas = new Canvas(mutableBitmap);

later i use this void 以后我用这个空

  public void drawneedle(){
    yourCanvas.rotate(speed, mutableBitmap.getWidth()/2, mutableBitmap.getHeight()/2);
    yourCanvas.save(Canvas.MATRIX_SAVE_FLAG);
    yourCanvas.drawBitmap(mutableBitmap, 0,0, null);
    yourCanvas.restore();
    iv.setImageBitmap(mutableBitmap);
}

Layout 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:mtr="http://schemas.android.com/apk/res/com.mindtherobot.samples.thermometer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top"
    android:orientation="vertical" >

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:max="270" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/spidometras" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="40sp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true" />

    </RelativeLayout>


</LinearLayout>

3369211

Do you clear the inner circle of the previously drawn content? 您是否清除了先前绘制内容的内圈?

You can do that with canvas.drawColor(0, Mode.CLEAR); 您可以使用canvas.drawColor(0, Mode.CLEAR);

Also, in my opinion, you need to add the configChanges flag to your activity's tag in the AndroidManifest.xml file, to prevent onCreate to run more than once when activity rotates. 另外,我认为,您需要将configChanges标志添加到AndroidManifest.xml文件中活动的标记中,以防止onCreate在活动旋转时多次运行。

android:configChanges="orientation|screenLayout|screenSize|smallestScreenSize"

To know that the activity rotated, you need to override the 要知道活动已轮换,您需要覆盖

onConfigurationChanged(Config newConfig) 

method and check the new orientation of the newConfig variable 方法并检查newConfig变量的新方向

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM