下載《安卓VS鴻蒙第三方件切換寶典》
由於字數較多,本文僅展示部分,查看完整版請點擊上方下載
眾所周知,安卓應用開發經過這么多年的發展相對成熟和穩定,鴻蒙OS作為后來者兼容一個成熟的開發體系會節省很多推廣和開發成本。但在實際開發中,代碼層面仍然有很多細節上的差異,會給初次開發人員造成困擾。
本寶典旨在匯總實際開發中第三方件接入時的代碼差異,以期幫助開發人員更好的進行開發作業,由於目前接觸的開發類型有限,所匯總的內容多少會有疏漏,后期我們會進一步完善和補全。
歡迎關注我們以及我們的專欄,方便您及時獲得相關內容的更新。
※基礎功能
1.獲取屏幕分辨率
安卓:
getWindowManager().getDefaultDisplay();
鴻蒙:
Optional<Display>
display = DisplayManager.getInstance().getDefaultDisplay(this.getContext());
Point pt = new Point();
display.get().getSize(pt);
2.隱藏標題欄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函數內增加如下代碼,注意要在setUIContent之前。
getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
7.獲取運行時權限
安卓:
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.頁面跳轉(顯示跳轉)
安卓:
A.從A跳轉至B,沒有參數,並且不接收返回值
Intent intent = new Intent();
intent.setClass(A.this, B.class);
startActivity(intent);
B.從A跳轉至B,有參數,不接收返回值
Intent intent = new Intent(this, B.class);
intent.putExtra(""name"", ""lily"");
startActivity(intent);
C.從A跳轉至B,有參數,接收返回值
Intent intent = new Intent(this, B.class);
intent.putExtra(""name"", ""lily"");
startActivityForResult(intent, 2);
鴻蒙:
A.從A跳轉至B,沒有參數,並且不接收返回值
present(new BSlice(), new Intent());
B.從A跳轉至B,有參數,不接收返回值
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跳轉至B,有參數,接收返回值
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.頁面跳轉(隱式跳轉)
安卓:
A.配置
<activity android:name="".B"">
<intent-filter>
<action android:name=""com.hly.view.fling""/>
</intent-filter>
</activity>
B.啟動
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函數中,增加頁面路由
addActionRoute( ""ability.intent.gotopage"", BSlice.class.getName());
C.跳轉
Intent intent = new Intent();
intent.setAction(""ability.intent.gotopage"");
startAbility(intent);
3.頁面碎片
安卓:
Fragment
鴻蒙:
Fraction
A:Ability繼承FractionAbility
B:獲取Fraction調度器
getFractionManager().startFractionScheduler()
C:構造Fraction
D:調用調度器管理Fraction
FractionScheduler.add()
FractionScheduler.remove()
FractionScheduler.replace()
備注:
參考demo
https://www.jianshu.com/p/58558dc6673a"
4.從xml文件創建一個組件實例
安卓:
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函數
鴻蒙:
Component.addDrawTask(Component.DrawTask task);
並實現Component.DrawTask接口的onDraw函數
6.自定義組件的自定義屬性(在xml中使用)
安卓:
需要3步
A.在 values/attrs.xml,在其中編寫 styleable 和 item 等標簽元素。
B.在layout.xml中,增加
xmln:app= ""http://schemas.android.com/apk/res/-auto""
C.在自定義組件的構造函數中,調用array.getInteger(R.styleable.***, 100);獲取屬性
鴻蒙:
只需2步
A. 在組件定義的layout.xml中增加 xmlns:app=""http://schemas.huawei.com/apk/res/ohos""
然后就可以使用app:***(***為任意字符串)來增加自定義屬性了,為了區分建議加上組件名前綴。
B. 在自定義組件的帶AttrSet參數的構造函數中,使用下面代碼獲取屬性。attrSet.getAttr(""***"").get().getStringValue();
7.觸摸事件
安卓:
android.view.MotionEvent
鴻蒙:
ohos.multimodalinput.event.TouchEvent
8.事件處理
安卓:
android.os.Handler
鴻蒙:
ohos.eventhandler.EventHandler
9.控件觸摸事件回調
安卓:
android.view.View.OnTouchListener
鴻蒙:
ohos.agp.components.Component.
TouchEventListener
10.輪播圖繼承的父類
安卓:
extends ViewPager
鴻蒙:
extends PageSlider
11.實現監聽輪播圖組件事件
安卓:
implements PageSlider.PageChangedListener
鴻蒙:
Implements OnPageChangedListener
12.touch事件監聽
安卓:
直接重寫onTouchEvent
鴻蒙:
繼承 Component.TouchEventListener然后在構造方法中設置監聽 setTouchEventListener(this::onTouchEvent);實現onTouchEvent
13.獲取點擊事件的坐標點
安卓:
event.getX(), event.getY()
鴻蒙:
MmiPoint point = touchEvent.getPointerPosition(touchEvent.getIndex());
14.調節滾輪中內容間距
安卓:
setLineSpacingMultiplier(float f)
鴻蒙:
setSelectedNormalTextMarginRatio(float f)
15.滾輪定位
安卓:
setPosition
鴻蒙:
setValue
16.Layout布局改變監聽
安卓:
View.OnLayoutChangeListener
鴻蒙:
Component.LayoutRefreshedListener
17.組件容器
安卓:
ViewGroup
鴻蒙:
ComponentContainer
18.添加組件
安卓:
addView()
鴻蒙:
addComponent()
19.動態列表的適配器
安卓:
extends RecyclerView.Adapter<>
鴻蒙:
extends RecycleItemProvider
20.動態列表
安卓:
RecyclerView
鴻蒙:
ListContainer
21.文本域動態監聽
安卓:
TextWatcher
鴻蒙:
Component.ComponentStateChangedListener
22.組件繪制自定義布局
安卓:
重寫onLayout(boolean changed, int left, int top, int right, int bottom)
鴻蒙:
重寫Component.LayoutRefreshedListener的onRefreshed方法
23.List組件
安卓:
ListView
鴻蒙:
ListContainer
24.設置背景顏色
安卓:
setBackgroundColor(maskColor);
鴻蒙:
// 創建背景元素
ShapeElement shapeElement = new ShapeElement();
// 設置顏色
shapeElement.setRgbColor(new RgbColor(255, 0, 0));
view.setBackground(shapeElement);
25.可以在控件上、下、左、右設置圖標,大小按比例自適應
安卓:
setCompoundDrawablesWithIntrinsicBounds
鴻蒙:
setAroundElements
26.RadioButton組件在xml中如何設置checked屬性
安卓:
在xml中可以設置
鴻蒙:
radioButton = findComponentById();
radioButton.setChecked(true);
備注:
sdk2.0后 xml中沒有了checked屬性,如果使用,可以在java代碼中實現"
27.文本域動態監聽
安卓:
TextWatcher
鴻蒙:
Component.ComponentStateChangedListener
28.顏色類
安卓:
java.awt.Color
鴻蒙:
ohos.agb.colors.rgbcolor
29.為ckeckbox或者Switch按鈕設置資源圖片
安卓:
略
鴻蒙:
VectorElement vectorElement = new VectorElement(this, ResourceTable.Graphic_candy);
setBackground(vectorElement)
30.子組件將拖拽事件傳遞給父組件
安卓:
略
鴻蒙:
注冊setDraggedListener偵聽,實現onDragPreAccept方法,再方法內根據拖拽方向判斷是否需要父組件處理,如果需要則返回false,否則返回true
※資源管理
1.管理資源
安卓:
AssertManager
鴻蒙:
ResourceManager
2.獲取應用的資源文件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()
下載《安卓VS鴻蒙第三方件切換寶典》
作者:軟通田可輝
想了解更多內容,請訪問: 51CTO和華為官方戰略合作共建的鴻蒙技術社區https://harmonyos.51cto.com
