天天看點

AndroidQQ5.0模仿

最近QQ5.0的Android實作版本有點火,我也出來湊湊熱鬧,希望對大家有所幫助。

QQ的示例圖如下:

AndroidQQ5.0模仿

自己模拟的示例圖如下:

AndroidQQ5.0模仿

QQ的模拟用到了幾個比較重要的知識點:

1.如何設定圓形圖形?

Android設定圖檔顯示的控件一般是方形的(矩形或者正方形),要想設定成圓形必須進行特殊的處理來設定成圓形。這裡我們用ImageView來顯示圓形圖檔,代碼如下:

public Bitmap getRoundedCornerBitmap(int drawableid) {

 Bitmap bitmap = BitmapFactory.decodeResource(getResources(),drawableid);

      Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(),

               bitmap.getHeight(), Config.ARGB_8888);//Bitmap.Config.ARGB_4444比Bitmap.Config.ARGB_8888更省記憶體

       Canvas canvas = new Canvas(outBitmap);

      final int color = 0xff424242;

       final Paint paint = new Paint();

       final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

        final RectF rectF = new RectF(rect);

        final float roundPX = bitmap.getWidth() / 2 < bitmap.getHeight() / 2 ? bitmap

                .getWidth() : bitmap.getHeight();

       paint.setAntiAlias(true);

        canvas.drawARGB(0, 0, 0, 0);

        paint.setColor(color);

        canvas.drawRoundRect(rectF, roundPX, roundPX, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

        canvas.drawBitmap(bitmap, rect, rect, paint);

       return outBitmap;

    }

上面的代碼大概的意思是先從Resource中的drawable中的得到drawable的Bitmap bitmap圖檔檔案,建立一個和上面Bitmap長和寬一樣的Bitmap outBitmap,最後bitmap的圓心部分的圖像用畫布畫到outBitmap中,傳回outBitmap。

用下面的代碼來設定圓形圖檔:

ImageView portait = (ImageView) view.findViewById(R.id.portrait);

portait.setImageBitmap(getRoundedCornerBitmap(R.drawable.head));

2.如何設定QQ的側滑菜單?

我參考的是孫國威 的【Android 案例分享】仿QQ5.0側滑菜單ResideMenu 

http://blog.csdn.net/manoel/article/details/39013095

在此謝過。

3.如何在Menu(菜單Fragment)和Content(内容Fragment)之間切換?

SlidingMenu  已經把我們的方法封裝地相當詳盡,可以用如下方法:

public void toggle() {

toggle(true);

}

SlidingMenu  sm;

初始化完成SlidingMenu之後,直接調用toggle方法就可以了:

sm.toggle();

Fragment中某個View的點選事件之後調用主Activity中的某個方法調用sm.toggle()就可以實作Fragment之間的切換:

view.findViewById(R.id.portrait).setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

((MainActivity) getActivity()).loadMenu();

}

});

4.如何把Fragment嵌套到Fragment中顯示的TabHost中?

Fragment的layout檔案不能具有<fragment>标簽,<fragment>的标簽可以放到FragmentActivity的layout檔案中來進行多個Fragment之間的切換。

主Content Fragment的layout如下:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

    android:id="@android:id/tabhost"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:layout_gravity="bottom"

    android:layout_alignParentBottom="true"

    android:layout_centerVertical="true" >

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="vertical" >

        <TabWidget

            android:id="@android:id/tabs"

            android:layout_width="match_parent"

            android:layout_height="wrap_content" >

        </TabWidget>

        <FrameLayout

            android:id="@android:id/tabcontent"

            android:layout_width="match_parent"

            android:layout_height="match_parent" >

   </FrameLayout>

    </LinearLayout>

</TabHost>

tabHost = (TabHost) view.findViewById(android.R.id.tabhost);

tabHost.setup();

               Fragment frag;

tabHost.setOnTabChangedListener(new OnTabChangeListener() {

@Override

public void onTabChanged(String tabId) {

FragmentTransaction ft = getFragmentManager()

.beginTransaction();

if (TextUtils.equals("first", tabId)) {

frag = new MessagesFragment();

} else if (TextUtils.equals("second", tabId)) {

frag = new ContactsFragment();

} else if (TextUtils.equals("third", tabId)) {

frag = new DynamicFragment();

}

ft.replace(android.R.id.tabcontent, frag, "frag");

ft.commit();

}

});

android.R.id.tabcontent 當點選不同的Tab的時候,将tabcontent中的内容可以替換為不同的Fragment,至此就實作了Fragment中的嵌套。

希望對大家有所幫助,如果有錯誤請您不吝指正。

Demo的下載下傳位址