After struggling with the charting function for a few days, I started developing a new feature. That is, share content to SMS, WeChat, Weibo and other channels. I have a simple task:
- Write a share button in Toolbar
- Draw a share page for Android
- Write a SMS Sharing Example
- Write a social sharing
On this day, I only completed the first three parts.
Share button on Toolbar
In Toolbar, the main thing is to draw the share button in the upper right corner with ImageView:
<?xml version="1.0" encoding="utf-8"?> <. xmlns:andro xmlns:toolbar="/apk/res-auto" xmlns:tools="/tools" android: android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimaryDark" android:gravity="center"> <TextView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="xxx" /> <ImageView android:visibility="invisible" android: 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" /> </.>
Then when loading the data, turn this element into visible:
();
SMS sharing example
Before implementing the UI, I wrote a simple sharing function:
@OnClick() void shareAction() { BaseShare smsShare = ("SMS"); String text = () + ":" + (); (this, text); }
It is then refactored into a simple factory pattern:
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; }
Corresponding to different sharing types, there are different classes to handle them accordingly.
Use Dialog to draw the bottom share
At the beginning, I used Dialog to draw the bottom layout:
void showShareDialog() { Dialog bottomDialog = new Dialog(this, ); View contentView = (this).inflate(.bottom_share, null); (contentView); layoutParams = (); = getResources().getDisplayMetrics().widthPixels; (layoutParams); ().setGravity(); (true); ().setWindowAnimations(.BottomDialog_Animation); (); }
Then I briefly learned about the animation effect:
<style name="BottomDialog"> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> </style> <style name="" parent=""> <item name="android:windowEnterAnimation">@anim/translate_dialog_in</item> <item name="android:windowExitAnimation">@anim/translate_dialog_out</item> </style>
Corresponding animation files:
translate_dialog_in:
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:andro 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:andro android:duration="300" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="0" android:toYDelta="100%"> </translate>
However, when drawing, some problems arise, that is, Dialog is on the top, and then use BottomSheetDialog to draw.
Draw a share menu using BottomSheetDialog
The corresponding logic has become simpler.
void showShareDialog() { final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(); View dialogView = ().inflate(.bottom_share, null); (.cancel_share).setOnClickListener(view -> { (); }); (dialogView); (); }
Summarize
The above is the Android implementation of SMS, WeChat, and Weibo sharing functions introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!