国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現短信、微信、微博分享功能

2019-10-22 18:18:51
字體:
來源:轉載
供稿:網友

在糾結了幾天的圖表功能之后,我開始開發一個新的功能。即分享內容到短信、微信、微博等渠道,對應的我有一個簡單的 Task:

  • 在 Toolbar 寫分享的按鈕
  • 繪制一個 Android 的分享頁面
  • 編寫短信分享示例
  • 編寫社交分享

在這一天,我只完成了前面的三部分。

Toolbar 上的分享按鈕

在 Toolbar 主要還是靠 ImageView 來繪制右上角的分享按鈕:

<?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>

然后在加載到數據的時候,將這個元素變為可見:

share.setVisibility(View.VISIBLE);

短信分享示例

在實現 UI 之前,我先寫了一個簡單的分享功能:

@OnClick(R.id.share)void shareAction() { BaseShare smsShare = ShareFactory.create("SMS"); String text = information.getTitle() + ":" + information.getTitle(); smsShare.share(this, text);}

隨后將其重構為簡單的工廠模式:

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;}

對應于不同的分享類型,都有不同的類來做相應的處理。

使用 Dialog 繪制底部分享

在最開始的時候,我使用的是 Dialog 來繪制底部的布局:

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(); }

然后簡單地了解了一下動畫效果:

<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>

對應的動畫文件:

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>

但是繪制的時候,出現了一些問題,即 Dialog 在最上面,隨后改用 BottomSheetDialog 來繪制。

使用 BottomSheetDialog 繪制分享菜單

對應的邏輯變得更加簡單了。

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();}

總結

以上所述是小編給大家介紹的Android實現短信、微信、微博分享功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 那曲县| 岳阳县| 德格县| 临洮县| 吉木萨尔县| 莲花县| 扎赉特旗| 微山县| 石城县| 肥东县| 旬阳县| 德安县| 内乡县| 连平县| 马鞍山市| 巨野县| 彰化市| 舟山市| 灵璧县| 南京市| 辛集市| 渭源县| 临洮县| 建始县| 遂川县| 新疆| 宜州市| 德格县| 龙州县| 保德县| 江都市| 昌邑市| 桂东县| 英吉沙县| 固阳县| 阿合奇县| 寿光市| 延庆县| 兴宁市| 砚山县| 普兰店市|