AndroidQQ5.0模仿

最近QQ5.0的Android实现版本有点火,我也出来凑凑热闹,希望对大家有所帮助。


QQ的示例图如下:


自己模拟的示例图如下:



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  已经把我们的方法封装地相当详尽,可以用如下方法:

/**
* Toggle the SlidingMenu. If it is open, it will be closed, and vice versa.
*/
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的下载地址





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值