安卓VS鴻蒙第三方件切換寶典 V1.0
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
眾所周知,安卓應(yīng)用開發(fā)經(jīng)過這么多年的發(fā)展相對(duì)成熟和穩(wěn)定,鴻蒙OS作為后來者兼容一個(gè)成熟的開發(fā)體系會(huì)節(jié)省很多推廣和開發(fā)成本。但在實(shí)際開發(fā)中,代碼層面仍然有很多細(xì)節(jié)上的差異,會(huì)給初次開發(fā)人員造成困擾。
本寶典旨在匯總實(shí)際開發(fā)中第三方件接入時(shí)的代碼差異,以期幫助開發(fā)人員更好的進(jìn)行開發(fā)作業(yè),由于目前接觸的開發(fā)類型有限,所匯總的內(nèi)容多少會(huì)有疏漏,后期我們會(huì)進(jìn)一步完善和補(bǔ)全。
歡迎關(guān)注我們以及我們的專欄,方便您及時(shí)獲得相關(guān)內(nèi)容的更新。
※基礎(chǔ)功能
1.獲取屏幕分辨率
安卓:
- getWindowManager().getDefaultDisplay();
鴻蒙:
- Optional<Display>
- display = DisplayManager.getInstance().getDefaultDisplay(this.getContext());
- Point pt = new Point();
- display.get().getSize(pt);
2.隱藏標(biāo)題欄TitleBar
安卓:
略
鴻蒙:
confi.json中添加如下描述:
- ""metaData"":{
- ""customizeData"":[
- {
- ""name"": ""hwc-theme"",
- ""value"": ""androidhwext:style/Theme.Emui.NoTitleBar"",
- ""extra"":""""
- }
- ]
- }
3.獲取屏幕密度
安卓:
- Resources.getSystem().getDisplayMetrics().density
鴻蒙:
- // 獲取屏幕密度
- Optional<Display>
- display = DisplayManager.getInstance().getDefaultDisplay(this.getContext());
- DisplayAttributes displayAttributes = display.get().getAttributes();
- //displayAttributes.xDpi;
- //displayAttributes.yDpi;
4.獲取上下文
安卓:
- context
鴻蒙:
- getContext()
5.組件的父類
安卓:
- android.view.View; class ProgressBar extends View
鴻蒙:
- class ProgressBar extends Component
6.沉浸式顯示
安卓:
略
鴻蒙:
A:在config.json ability 中添加
- "metaData"": {
- ""customizeData"": [
- {
- ""extra"": """",
- ""name"": ""hwc-theme"",
- ""value"": ""androidhwext:style/Theme.Emui.Light.NoTitleBar""
- }
- ]
- }
B:在AbilitySlice的onStart函數(shù)內(nèi)增加如下代碼,注意要在setUIContent之前。
- getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
7.獲取運(yùn)行時(shí)權(quán)限
安卓:
- ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 1)
鴻蒙:
- requestPermissionsFromUser(
- new String[]{""ohos.permission.READ_MEDIA"", ""ohos.permission.WRITE_MEDIA"", ""ohos.permission.READ_USER_STORAGE"", ""ohos.permission.WRITE_USER_STORAGE"",}, 1);
※布局&組件
1.頁面跳轉(zhuǎn)(顯示跳轉(zhuǎn))
安卓:
A.從A跳轉(zhuǎn)至B,沒有參數(shù),并且不接收返回值
- Intent intent = new Intent();
- intent.setClass(A.this, B.class);
- startActivity(intent);
B.從A跳轉(zhuǎn)至B,有參數(shù),不接收返回值
- Intent intent = new Intent(this, B.class);
- intent.putExtra(""name"", ""lily"");
- startActivity(intent);
C.從A跳轉(zhuǎn)至B,有參數(shù),接收返回值
- Intent intent = new Intent(this, B.class);
- intent.putExtra(""name"", ""lily"");
- startActivityForResult(intent, 2);
鴻蒙:
A.從A跳轉(zhuǎn)至B,沒有參數(shù),并且不接收返回值
- present(new BSlice(), new Intent());
B.從A跳轉(zhuǎn)至B,有參數(shù),不接收返回值
- Intent intent = new Intent();
- Operation operation = new Intent.OperationBuilder()
- .withDeviceId("""") .withBundleName(""com.test"") .withAbilityName(""com.test.BAbility"")
- .build();
- intent.setParam(""name"",""lily"");
- intent.setOperation(operation);
- startAbility(intent);
C.從A跳轉(zhuǎn)至B,有參數(shù),接收返回值
- Intent intent = new Intent();
- Operation operation = new Intent.OperationBuilder()
- .withDeviceId("""") .withBundleName(""com.test"") .withAbilityName(""com.test.BAbility"")
- .build();
- intent.setParam(""name"",""lily"");
- intent.setOperation(operation);
- startAbilityForResult(intent,100);
2.頁面跳轉(zhuǎn)(隱式跳轉(zhuǎn))
安卓:
A.配置
- <activity android:name="".B"">
- <intent-filter>
- <action android:name=""com.hly.view.fling""/>
- </intent-filter>
- </activity>
B.啟動(dòng)
- Intent intent = new Intent(); intent.setAction(""com.hly.view.fling""); intent.putExtra(""key"", ""name""); startActivity(intent);
鴻蒙:
A.在config.json文件ability 中添加以下信息
- "skills"":[
- {
- ""actions"":[
- ""ability.intent.gotopage""
- ]
- }
- ]
B.在MainAbility的onStart函數(shù)中,增加頁面路由
- addActionRoute( ""ability.intent.gotopage"", BSlice.class.getName());
C.跳轉(zhuǎn)
- Intent intent = new Intent();
- intent.setAction(""ability.intent.gotopage"");
- startAbility(intent);
3.頁面碎片
安卓:
- Fragment
鴻蒙:
- Fraction
A:Ability繼承FractionAbility
B:獲取Fraction調(diào)度器
- getFractionManager().startFractionScheduler()
C:構(gòu)造Fraction
D:調(diào)用調(diào)度器管理Fraction
- FractionScheduler.add()
- FractionScheduler.remove()
- FractionScheduler.replace()
備注:
參考demo
https://www.jianshu.com/p/58558dc6673a"
4.從xml文件創(chuàng)建一個(gè)組件實(shí)例
安卓:
- LayoutInflater.from(mContext).inflate(R.layout.banner_viewpager_layout, null);
鴻蒙:
- LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_ability_main, null, false);
5.組件自定義繪制
安卓:
- ImageView.setImageDrawable(Drawable drawable);
并重寫Drawable 的draw函數(shù)
鴻蒙:
- Component.addDrawTask(Component.DrawTask task);
并實(shí)現(xiàn)Component.DrawTask接口的onDraw函數(shù)
6.自定義組件的自定義屬性(在xml中使用)
安卓:
需要3步
A.在 values/attrs.xml,在其中編寫 styleable 和 item 等標(biāo)簽元素。
B.在layout.xml中,增加
- xmln:app= ""http://schemas.android.com/apk/res/-auto""
C.在自定義組件的構(gòu)造函數(shù)中,調(diào)用array.getInteger(R.styleable.***, 100);獲取屬性
鴻蒙:
只需2步
A. 在組件定義的layout.xml中增加 xmlns:app=""http://schemas.huawei.com/apk/res/ohos""
然后就可以使用app:***(***為任意字符串)來增加自定義屬性了,為了區(qū)分建議加上組件名前綴。
B. 在自定義組件的帶AttrSet參數(shù)的構(gòu)造函數(shù)中,使用下面代碼獲取屬性。attrSet.getAttr(""***"").get().getStringValue();
7.觸摸事件
安卓:
- android.view.MotionEvent
鴻蒙:
- ohos.multimodalinput.event.TouchEvent
8.事件處理
安卓:
- android.os.Handler
鴻蒙:
- ohos.eventhandler.EventHandler
9.控件觸摸事件回調(diào)
安卓:
- android.view.View.OnTouchListener
鴻蒙:
- ohos.agp.components.Component.
- TouchEventListener
10.輪播圖繼承的父類
安卓:
- extends ViewPager
鴻蒙:
- extends PageSlider
11.實(shí)現(xiàn)監(jiān)聽輪播圖組件事件
安卓:
- implements PageSlider.PageChangedListener
鴻蒙:
- Implements OnPageChangedListener
12.touch事件監(jiān)聽
安卓:
- 直接重寫onTouchEvent
鴻蒙:
- 繼承 Component.TouchEventListener然后在構(gòu)造方法中設(shè)置監(jiān)聽 setTouchEventListener(this::onTouchEvent);實(shí)現(xiàn)onTouchEvent
13.獲取點(diǎn)擊事件的坐標(biāo)點(diǎn)
安卓:
- event.getX(), event.getY()
鴻蒙:
- MmiPoint point = touchEvent.getPointerPosition(touchEvent.getIndex());
14.調(diào)節(jié)滾輪中內(nèi)容間距
安卓:
- setLineSpacingMultiplier(float f)
鴻蒙:
- setSelectedNormalTextMarginRatio(float f)
15.滾輪定位
安卓:
- setPosition
鴻蒙:
- setValue
16.Layout布局改變監(jiān)聽
安卓:
- View.OnLayoutChangeListener
鴻蒙:
- Component.LayoutRefreshedListener
17.組件容器
安卓:
- ViewGroup
鴻蒙:
- ComponentContainer
18.添加組件
安卓:
- addView()
鴻蒙:
- addComponent()
19.動(dòng)態(tài)列表的適配器
安卓:
- extends RecyclerView.Adapter<>
鴻蒙:
- extends RecycleItemProvider
20.動(dòng)態(tài)列表
安卓:
- RecyclerView
鴻蒙:
- ListContainer
21.文本域動(dòng)態(tài)監(jiān)聽
安卓:
- TextWatcher
鴻蒙:
- Component.ComponentStateChangedListener
22.組件繪制自定義布局
安卓:
- 重寫onLayout(boolean changed, int left, int top, int right, int bottom)
鴻蒙:
- 重寫Component.LayoutRefreshedListener的onRefreshed方法
23.List組件
安卓:
- ListView
鴻蒙:
- ListContainer
24.設(shè)置背景顏色
安卓:
- setBackgroundColor(maskColor);
鴻蒙:
- // 創(chuàng)建背景元素
- ShapeElement shapeElement = new ShapeElement();
- // 設(shè)置顏色
- shapeElement.setRgbColor(new RgbColor(255, 0, 0));
- view.setBackground(shapeElement);
25.可以在控件上、下、左、右設(shè)置圖標(biāo),大小按比例自適應(yīng)
安卓:
- setCompoundDrawablesWithIntrinsicBounds
鴻蒙:
- setAroundElements
26.RadioButton組件在xml中如何設(shè)置checked屬性
安卓:
在xml中可以設(shè)置
鴻蒙:
- radioButton = findComponentById();
- radioButton.setChecked(true);
備注:
sdk2.0后 xml中沒有了checked屬性,如果使用,可以在java代碼中實(shí)現(xiàn)"
27.文本域動(dòng)態(tài)監(jiān)聽
安卓:
- TextWatcher
鴻蒙:
- Component.ComponentStateChangedListener
28.顏色類
安卓:
- java.awt.Color
鴻蒙:
- ohos.agb.colors.rgbcolor
29.為ckeckbox或者Switch按鈕設(shè)置資源圖片
安卓:
略
鴻蒙:
- VectorElement vectorElement = new VectorElement(this, ResourceTable.Graphic_candy);
- setBackground(vectorElement)
30.子組件將拖拽事件傳遞給父組件
安卓:
略
鴻蒙:
注冊(cè)setDraggedListener偵聽,實(shí)現(xiàn)onDragPreAccept方法,再方法內(nèi)根據(jù)拖拽方向判斷是否需要父組件處理,如果需要?jiǎng)t返回false,否則返回true
※資源管理
1.管理資源
安卓:
- AssertManager
鴻蒙:
- ResourceManager
2.獲取應(yīng)用的資源文件rawFile,并返回InputStream
安卓:
- getResources()
- AssetManager類
鴻蒙:
- ResourceManager resourceManager = getContext().getResourceManager();
- RawFileEntry rawFileEntry = resourceManager.getRawFileEntry(jsonFile);
- Resource resource = null;
- try {
- resource = rawFileEntry.openRawFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
備注:
- Resource是InputStream的子類,可以直接作為InputStream使用。"
3.獲取文件路徑
安卓:
- Environment.getExternalStorageDirectory().getAbsolutePath()
鴻蒙:
- 獲取文檔(DIRECTORY_DOCUMENTS)、下載(DIRECTORY_DOWNLOADS)、視頻(DIRECTORY_MOVIES)、音樂(DIRECTORY_MUSIC)、圖片(DIRECTORY_PICTURES)
- GetExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath()
※消息&多線程
1.事件循環(huán)器
安卓:
- android.os.Looper
鴻蒙:
- EventRunner.create(true)
備注:
有參數(shù)且為true,表示隊(duì)列被托管,參數(shù)為false,或無參表示不被托管,需要eventRunner.run()調(diào)用
2.消息
安卓:
- android.os.Message
鴻蒙:
- InnerEvent
3.休眠
安卓:
- android.os.SystemClock.sleep()
鴻蒙:
- ohos.miscservices.timeutility.time;
- Time.sleep(int millesend)
4.事件通知延遲消息
安卓:
- Handler.postDelayed(MESSAGE_LOGIN, 5000);
鴻蒙:
- Handler.postTask(task, 5000);
5.Intent傳遞參數(shù)
安卓:
- Intent.putExtra or add Bundle
鴻蒙:
- Intent.setParam
6.消息發(fā)送
安卓:
- Handler handler = new Handler,通過handlerMsg發(fā)消息
鴻蒙:
- InnerEvent event1 = InnerEvent.get(eventId1, param, object);
- myHandler.sendEvent(event1, 0, Priority.IMMEDIATE);
7.更新UI
安卓:
- class MyHandle extends Handler{
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- switch (msg.what) {
- case 1:
- //更新UI的操作
- break;
- default:
- Break;
- }
- }
- }
鴻蒙:
- abilitySlice.getUITaskDispatcher().asyncDispatch(() -> {
- //更新UI的操作
- });
※圖片處理
1.位圖資源
安卓:
- Bitmap
鴻蒙:
- PixelMap
2.圖像縮放,拉伸到視圖邊界
安卓:
- ImageView.ScaleType
- image.setScaleType(ScaleType.FXY);
鴻蒙:
- Image.ScaleMode
- image.setScaleMode(Image.ScaleMode.STRETCH);
3.List組件&內(nèi)容適配器
安卓:
- ListView
- extends BaeAdapter
- ViewPage.setAdapter(BaeAdapter);
鴻蒙:
- ListContainer
- extends PageSlider
- PageSlider.setProvider(PageSlider);
4.圖片顯示組件
安卓:
- androidx.appcompat.widget.AppCompatImageView
- Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),drawableId);
- Image.setImageBitmap(bitmap);
鴻蒙:
- ohos.agp.components.Image
根據(jù)實(shí)際情況可傳遞其他參數(shù)
- ImageSource imageSource = ImageSource.create(file, new ImageSource.SourceOptions());
- pixelMap = imageSource.createPixelmap(new ImageSource.DecodingOptions());
- image.setPixelMap(pixelMap);
5.圖片填充整個(gè)控件
安卓:
- image.setScaleType(ScaleType.FXY);
鴻蒙:
- image.setScaleMode(Image.ScaleMode.STRETCH);
6.通過資源id獲取位圖
安卓:
- getBitmapFromDrawable
鴻蒙:
- /**
- * 通過資源ID獲取位圖對(duì)象
- **/
- private PixelMap getPixelMap(int resId) {
- InputStream drawableInputStream = null;
- try {
- drawableInputStream = getResourceManager().getResource(resId);
- ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
- sourceOptions.formatHint = ""image/png"";
- ImageSource imageSource = ImageSource.create(drawableInputStream, null);
- ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
- decodingOptions.desiredSize = new Size(0, 0);
- decodingOptions.desiredRegion = new Rect(0, 0, 0, 0);
- decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
- PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
- return pixelMap;
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (drawableInputStream != null) {
- drawableInputStream.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return null;
- }
7.獲取Gif圖片幀
安卓:
需要自定frame類,通過decoder獲取
鴻蒙:
- ImageSource.createPixelmap(int index, ImageSource.DecodingOptions opts)
8.BMP位圖裁剪
安卓:
- Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height)
鴻蒙:
- PixelMap.create(PixelMap source, Rect srcRegion, PixelMap.InitializationOptions opts)
※視頻播放
在視頻播放窗口上層增加控件
安卓:
略
鴻蒙:
A.pinToTop設(shè)置false,保證其他控件與surfaceProvider在同一layout下,并且不能設(shè)置背景
B.增加以下代碼設(shè)置頂部窗口透明
- WindowManager.getInstance().getTopWindow().get().setTransparent(true);
※數(shù)據(jù)庫
數(shù)據(jù)庫獲取索引
安卓:
- android.database.Cursor;
- cursor.getString()/cursor.getColumnIndex()
鴻蒙:
- ohos.data.resultset
※數(shù)據(jù)結(jié)構(gòu)
1.應(yīng)用程序數(shù)據(jù)共享
安卓:
- context.getContentResolver();
- resolver.getType(uri)
鴻蒙:
- ohos.aafwk.ability.DataAbilityHelper
2.JSON解析
安卓:
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import org.json.JSONTokener;
鴻蒙:
- Gson,fastJson
3.對(duì)象序列化
安卓:
- android.os.Parcel;
- parcel.readParcelable();parcel.writeParcelable()
鴻蒙:
- ohos.utils.parcel
4.浮點(diǎn)數(shù)矩形,獲取中心點(diǎn)
安卓:
- RectF.centerX()
鴻蒙:
- RectFloat.getCenter().getPointX()
5.數(shù)據(jù)結(jié)構(gòu)類
安卓:
- LongSparseArray
- SparseArrayCompat
鴻蒙:
使用HashMap
備注:
內(nèi)存使用和查找性能會(huì)有影響。"
6.浮點(diǎn)數(shù)矩形
安卓:
- RectF
鴻蒙:
- RectFloat
7.浮點(diǎn)坐標(biāo)
安卓:
- PointF
鴻蒙:
- 可使用Point
※對(duì)話框
1.對(duì)話框銷毀
安卓:
- mDialog.dismiss()
鴻蒙:
- mDialog.destroy();
2.對(duì)話框中加載布局
安卓:
- mDialog.setContentView(ViewGroup dialogView)
鴻蒙:
- setContentCustomComponent(Componnet comp)
3.點(diǎn)擊對(duì)話框外部關(guān)閉對(duì)話框
安卓:
- mDialog.setCancelable(mPickerOptions.cancelable)
鴻蒙:
- mDialog.setDialogListener(new BaseDialog.DialogListener() {
- @Override
- public boolean isTouchOutside() {
- mDialog.destroy(); dialogView.getComponentParent().removeComponent(dialogView);
- return true;
- }
- });
備注:
鴻蒙對(duì)話框銷毀之后需移除對(duì)話框中加載的布局,否則再次加載會(huì)報(bào)錯(cuò)"
※動(dòng)畫
1.旋轉(zhuǎn)動(dòng)畫
安卓:
- android.view.animation.RotateAnimation
鴻蒙:
- ohos.agp.animation.AnimatorProperty
2.值動(dòng)畫及相關(guān)回調(diào)
安卓:
- android.animation.ValueAnimator
- ValueAnimator.AnimatorUpdateListener
- Animator.AnimatorListener
- Animator.AnimatorPauseListener
鴻蒙:
- ohos.agp.animation.AnimatorValue
- AnimatorValue.ValueUpdateListener
- Animator.StateChangedListener
- Animator.LoopedListener
備注:
啟動(dòng)動(dòng)畫時(shí),AnimatorValue必須作為類的成員變量,而不能時(shí)函數(shù)局部變量,否則動(dòng)畫不會(huì)啟動(dòng)"
3.線性插值器
安卓:
- LinearInterpolator
鴻蒙:
自己寫一個(gè)
- public interface Interpolator {
- float getInterpolation(float input);
- }
- public class LinearInterpolator implements Interpolator {
- public LinearInterpolator() {
- }
- public LinearInterpolator(Context context, AttrSet attrs) {
- }
- public float getInterpolation(float input) {
- return input;
- }
- }
4.設(shè)置動(dòng)畫循環(huán)次數(shù)
安卓:
- animation.setRepeatCount(Animation.INFINITE)
鴻蒙:
- animator.setLoopedCount(Animator.INFINITE)
※存儲(chǔ)
獲取存儲(chǔ)根路徑
安卓:
- Environment.getExternalStorageDirectory().getAbsolutePath();
鴻蒙:
- System.getProperty("user.dir")
※Canvas繪圖
1.繪制圓弧
安卓:
- Android canvas.drawArc()
鴻蒙:
- ohos.agp.render; class Arc
2.繪制圓形的兩種方式
安卓:
- canvas.drawCircle(float x, float y, float radius, Paint paint)
鴻蒙:
- A.canvas.drawPixelMapHolderRoundRectShape(PixelMapHolder holder, RectFloat rectSrc, RectFloat rectDst, float radiusX, float radiusY)
- B.canvas.drawCircle(float x, float y, float radius, Paint paint)
3.繪制文本的方法
安卓:
- drawText (String text, float x, float y, Paint paint)
鴻蒙:
- drawText(Paint paint, String text, float x, float y)
4.獲取text 的寬度
安卓:
- text.getWidth();
鴻蒙:
- Paint paint = new Paint();
- paint.setTextSize(text.getTextSize());
- float childWidth = paint.measureText(text.getText());
歡迎交流:HWIS-HOS@isoftstone.com
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)