效果圖
類庫的介紹
org.dync.giftlibrary.widget
GiftAnimationUtil.java 動畫類
GiftControl.java 給外部調用的類(核心)
GiftFrameLayout.java 禮物布局類
GiftModel.java 給禮物布局填充數據類
以上是禮物動畫一(推薦使用禮物動畫一,在demo中的Gift1Activity.java使用)LeftGiftControl.java 給外部調用的類(核心)
LeftGiftsItemLayout.java 禮物布局類
GiftModel.java 給禮物布局填充數據類
以上是禮物動畫二(可以借鑒,在demo中的Gift2Activity.java使用)
個人建議使用Gift1Activity項目中的庫,Gift2Activity項目中的庫后面不怎么維護了
1:到GitHub 把項目clone到本地。
2: 把giftlibrary庫依賴到你的項目中去
3:在你要顯示的xml文件中添加展示禮物和禮物面板的地方 以項目中的activity_gift1.xml為例
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:src="@mipmap/ic_bg" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:orientation="vertical"> <org.dync.giftlibrary.widget.GiftFrameLayout android:id="@+id/gift_layout1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <org.dync.giftlibrary.widget.GiftFrameLayout android:id="@+id/gift_layout2" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <Button android:id="@+id/action" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:text="禮物面板顯示/隱藏" /> <LinearLayout android:id="@+id/bottom" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="vertical"> <include layout="@layout/chat_tool_box" /> </LinearLayout> </RelativeLayout>
上面的GiftFrameLayout是展示禮物的控件,我這里僅展示兩條,你可以添加多個禮物同時展示,但是你需要GiftControl類中相應的修改代碼來實現。同時禮物面板可以使用DialogFragment來替代我這里。
4:在activity中找到控件后就可以初始化禮物模塊了。
a.禮物面板。
代碼如下:
GiftPanelControl giftPanelControl = new GiftPanelControl(this, mViewpager, mRecyclerView, mDotsLayout); giftPanelControl.setGiftListener(new GiftPanelControl.GiftListener() { @Override public void getGiftStr(String giftStr) { giftstr = giftStr; } });
這里的giftStr參數我傳的是資源文件中圖片的名稱,你也可以傳的是圖片的id,這里主要是辨別發送的那個禮物。
b.展示禮物
把禮物布局控件傳遞給禮物控制器
giftControl = new GiftControl(Gift1Activity.this); giftControl.setGiftLayout(giftFrameLayout1, giftFrameLayout2);
c.顯示禮物數量的面板
tvGiftNum.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View v) { showGiftDialog(); } });
d.禮物面板中發送按鈕發送禮物的操作
btnGift.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (TextUtils.isEmpty(giftstr)) { Toast.makeText(getApplication(), "你還沒選擇禮物呢", Toast.LENGTH_SHORT).show(); } else { String numStr = tvGiftNum.getText().toString(); if (!TextUtils.isEmpty(numStr)) { int giftnum = Integer.parseInt(numStr); if (giftnum == 0) { return; } else { giftControl.loadGift(new GiftModel(giftstr, "安卓機器人", giftnum, "http://www.baidu.com", "123", "Lee123", "http://www.baidu.com")); } } } } });
e.簡單的操作了橫豎屏顯示不同的面板
在AndroidManifest.xml中給Activity配置
<activity
android:name=".Gift1Activity" android:configChanges="orientation|keyboardHidden|screenSize" />
然后在Activity重寫onConfigurationChanged(Configuration newConfig)方法
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 橫屏 // Log.e(TAG, "onConfigurationChanged: " + "橫屏"); onConfigurationLandScape(); } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { // Log.e(TAG, "onConfigurationChanged: " + "豎屏"); onConfigurationPortrait(); } } private void onConfigurationPortrait() { ll_portrait.setVisibility(View.VISIBLE); ll_landscape.setVisibility(View.GONE); } private void onConfigurationLandScape() { ll_portrait.setVisibility(View.GONE); ll_landscape.setVisibility(View.VISIBLE); }
findViewById(R.id.action).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (giftLayout.getVisibility() == View.VISIBLE) { giftLayout.setVisibility(View.GONE); } else { giftLayout.setVisibility(View.VISIBLE); } } });