绘制位图并在FrameLayout中使用它

时间:2014-04-18 11:43:05

标签: android canvas bitmap imageview android-framelayout

我在框架布局中有2个图像视图。我的目标是覆盖第一个图像视图,每5秒钟显示第二个图像视图,因此它显示为着色图像动画。

我的代码运行顺畅,但我看不到图像相互重叠。 请告知我在这里失踪的内容,并非常感谢任何帮助。 谢谢。

layout.xml

 <FrameLayout
    android:id="@+id/rooster_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

   <ImageView
        android:id="@+id/rooster"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:contentDescription="@string/desc_list_item_icon"
        android:padding="10dp"
        android:src="@drawable/rooster"/>

   <ImageView
        android:id="@+id/rooster_layer"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:contentDescription="@string/desc_list_item_icon"
        android:padding="10dp" />  
</FrameLayout>

片段类

    public class SleekUserDetailFragment extends Fragment {

    private static String TAG = SleekUserDetailFragment.class
            .getCanonicalName();
    private static int FINISH = 100;
    private int start = 0;
    private static int CURRENT = 50;

    private Activity activity;
    private ImageView rooster;
    private ImageView roosterLayer;
    private View mainView;
    private FrameLayout roosterContainer;
    private Bitmap outputRooster;
    private Bitmap roosterClip;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        activity = getActivity();
        mainView = inflater.inflate(R.layout.sleek_user_detail_fragment,
                container, false);

        roosterContainer = (FrameLayout) mainView.findViewById(R.id.rooster_container);
        rooster = (ImageView) mainView.findViewById(R.id.rooster);
        roosterLayer = (ImageView) mainView.findViewById(R.id.rooster_layer);
        roosterClip = BitmapFactory.decodeResource(activity.getResources(),
                R.drawable.rooster_clip);

        Thread t = new Thread() {
            public void run() {
                while (start <= CURRENT) {
                    Log.e(TAG, "START = " + start);
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.e(TAG, "RUN THREAD UPDATE");
                            updateRooster(start, start, start + 10);
                        }
                    });

                    try {
                        Thread.sleep(5 * 1000);
                    } catch (InterruptedException e) {
                        Log.e(TAG,e.getMessage());
                    }
                }
            }
        };
        t.start();
        return mainView;
    }

    private void updateRooster(int width, int height, int percentage) {
        Paint paint = new Paint();
        outputRooster = null;
        outputRooster = Bitmap.createBitmap(roosterClip.getWidth(),
                roosterClip.getHeight(), Config.ARGB_8888);

        Canvas canvas = new Canvas(outputRooster);
        final Rect rectClip = new Rect(0, 0, roosterClip.getWidth()
                * (percentage / 100), roosterClip.getHeight()
                * (percentage / 100));
        canvas.drawBitmap(roosterClip, rectClip, rectClip, paint);

        roosterContainer.removeView(roosterLayer);
        roosterLayer.setImageBitmap(outputRooster);
        Log.e(TAG, "roosterlayer bitmap = "+((BitmapDrawable)roosterLayer.getDrawable()).getBitmap());
        roosterContainer.addView(roosterLayer);
        start = start + 10;
    }
}

3 个答案:

答案 0 :(得分:0)

使用RelativeLayout而不是framelayout

答案 1 :(得分:0)

示例:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > 
<ImageView android:id="@id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/btn_dialog"/>
<ImageView android:id="@id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/btn_radio"/>
 </RelativeLayout>

答案 2 :(得分:0)

找到我的错误,图片不会因为下面的线而出现:

roosterClip.getWidth() * (percentage / 100)

将该行更改为:

roosterClip.getWidth() * ((float)percentage / 100)

我得到了预期的结果。也许它会帮助别人。