Android實(shí)現(xiàn)短信、微信、微博分享功能
在糾結(jié)了幾天的圖表功能之后,我開(kāi)始開(kāi)發(fā)一個(gè)新的功能。即分享內(nèi)容到短信、微信、微博等渠道,對(duì)應(yīng)的我有一個(gè)簡(jiǎn)單的 Task:
- 在 Toolbar 寫(xiě)分享的按鈕
- 繪制一個(gè) Android 的分享頁(yè)面
- 編寫(xiě)短信分享示例
- 編寫(xiě)社交分享
在這一天,我只完成了前面的三部分。
Toolbar 上的分享按鈕
在 Toolbar 主要還是靠 ImageView 來(lái)繪制右上角的分享按鈕:
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:toolbar="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimaryDark" android:gravity="center"> <TextView android:id="@+id/toolbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="xxx" /> <ImageView android:visibility="invisible" android:id="@+id/share" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingEnd="@dimen/length_24" android:paddingStart="@dimen/length_16" android:paddingTop="@dimen/length_16" android:paddingBottom="@dimen/length_16" android:layout_gravity="right" android:src="@drawable/share_icon" tools:ignore="RtlHardcoded" /> </android.support.v7.widget.Toolbar>
然后在加載到數(shù)據(jù)的時(shí)候,將這個(gè)元素變?yōu)榭梢?jiàn):
share.setVisibility(View.VISIBLE);
短信分享示例
在實(shí)現(xiàn) UI 之前,我先寫(xiě)了一個(gè)簡(jiǎn)單的分享功能:
@OnClick(R.id.share) void shareAction() { BaseShare smsShare = ShareFactory.create("SMS"); String text = information.getTitle() + ":" + information.getTitle(); smsShare.share(this, text); }
隨后將其重構(gòu)為簡(jiǎn)單的工廠模式:
public static BaseShare getShareType(String type) { switch (type) { case "SMS": return new SMSShare(); case "WEIBO": return new WeiboShare(); case "MOMENTS": return new MomentsShare(); case "WECHAT": return new WechatShare(); } return null; }
對(duì)應(yīng)于不同的分享類(lèi)型,都有不同的類(lèi)來(lái)做相應(yīng)的處理。
使用 Dialog 繪制底部分享
在最開(kāi)始的時(shí)候,我使用的是 Dialog 來(lái)繪制底部的布局:
void showShareDialog() { Dialog bottomDialog = new Dialog(this, R.style.BottomDialog); View contentView = LayoutInflater.from(this).inflate(R.layout.bottom_share, null); bottomDialog.setContentView(contentView); ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams(); layoutParams.width = getResources().getDisplayMetrics().widthPixels; contentView.setLayoutParams(layoutParams); bottomDialog.getWindow().setGravity(Gravity.BOTTOM); bottomDialog.setCanceledOnTouchOutside(true); bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation); bottomDialog.show(); }
然后簡(jiǎn)單地了解了一下動(dòng)畫(huà)效果:
<style name="BottomDialog"> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> </style> <style name="BottomDialog.Animation" parent="Animation.AppCompat.Dialog"> <item name="android:windowEnterAnimation">@anim/translate_dialog_in</item> <item name="android:windowExitAnimation">@anim/translate_dialog_out</item> </style>
對(duì)應(yīng)的動(dòng)畫(huà)文件:
translate_dialog_in:
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromXDelta="0" android:fromYDelta="100%" android:toXDelta="0" android:toYDelta="0"> </translate>
translate_dialog_out:
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="0" android:toYDelta="100%"> </translate>
但是繪制的時(shí)候,出現(xiàn)了一些問(wèn)題,即 Dialog 在最上面,隨后改用 BottomSheetDialog 來(lái)繪制。
使用 BottomSheetDialog 繪制分享菜單
對(duì)應(yīng)的邏輯變得更加簡(jiǎn)單了。
void showShareDialog() { final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(DetailActivity.this); View dialogView = LayoutInflater.from(InformationDetailActivity.this).inflate(R.layout.bottom_share, null); dialogView.findViewById(R.id.cancel_share).setOnClickListener(view -> { bottomSheetDialog.dismiss(); }); bottomSheetDialog.setContentView(dialogView); bottomSheetDialog.show(); }
總結(jié)
以上所述是小編給大家介紹的Android實(shí)現(xiàn)短信、微信、微博分享功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android抽屜布局DrawerLayout的簡(jiǎn)單使用
這篇文章主要為大家詳細(xì)介紹了Android抽屜布局DrawerLayout的簡(jiǎn)單使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06Android開(kāi)發(fā)flow常見(jiàn)API的使用示例詳解
這篇文章主要為大家介紹了Android開(kāi)發(fā)flow常見(jiàn)API的使用示例詳解,希望能夠幫助大家更好的掌握f(shuō)low使用,熟練的應(yīng)用于各種場(chǎng)景,祝大家多多進(jìn)步,早日升職加薪2022-08-08Android使用觀察者模式Observer實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)監(jiān)聽(tīng)
這篇文章主要為大家詳細(xì)介紹了Android使用觀察者模式Observer實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)監(jiān)聽(tīng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android提高Service優(yōu)先級(jí)的方法分析
這篇文章主要介紹了Android提高Service優(yōu)先級(jí)的方法,簡(jiǎn)單講述了Service優(yōu)先級(jí)的功能,并對(duì)比分析了1.5與1.0設(shè)置Service的技巧,需要的朋友可以參考下2016-06-06Android View事件機(jī)制 21問(wèn)21答
這篇文章主要介紹了Android View事件機(jī)制 21問(wèn)21答 的相關(guān)資料,需要的朋友可以參考下2016-02-02Android中RecyclerView實(shí)現(xiàn)橫向滑動(dòng)代碼
這篇文章主要介紹了Android中RecyclerView實(shí)現(xiàn)橫向滑動(dòng)代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07android實(shí)現(xiàn)記住用戶(hù)名和密碼以及自動(dòng)登錄
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)記住用戶(hù)名和密碼以及自動(dòng)登錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Android實(shí)現(xiàn)同頻共幀動(dòng)畫(huà)效果
我們聽(tīng)過(guò)“同頻共振”,其原理是多個(gè)物體物體以同樣的頻率振動(dòng),但是本篇實(shí)現(xiàn)的效果是“同頻共幀”,含義是:動(dòng)畫(huà)以同樣的頻率和同樣的幀展示在多個(gè)不同View上,文中通過(guò)代碼示例介紹的非常詳細(xì),感興趣的同學(xué)可以自己動(dòng)手嘗試一下2024-01-01解決Android-RecyclerView列表倒計(jì)時(shí)錯(cuò)亂問(wèn)題
這篇文章主要介紹了解決Android-RecyclerView列表倒計(jì)時(shí)錯(cuò)亂問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08