diff --git a/app/Activity.md b/app/Activity.md index 5d7b9ce3026283daf2aa144933585b4ede4964d1..b1d74d1fb5326b4bfc3e14db0df8bfb87be35bbe 100644 --- a/app/Activity.md +++ b/app/Activity.md @@ -27,3 +27,35 @@ protected void onOrientationChanged(AbilityInfo.DisplayOrientation displayOrient } ``` +### **TouchEvent** +>+ openharmony API: ohos.multimodalinput.event.TouchEvent +>+ openharmony SDK版本:2.1.1.21 +>+ IDE版本:2.1.0.501 +>+ 实现方案: + +openharmony: + +实现TouchEventListener接口,重写onTouchEvent方法 +``` java + public class Text extends Component implements Component.TouchEventListener{ + + @Override + public boolean onTouchEvent(Component component, TouchEvent touchEvent) { + int action = touchEvent.getAction(); + MmiPoint point = touchEvent.getPointerPosition(touchEvent.getIndex()); + switch (action){ + case TouchEvent.PRIMARY_POINT_DOWN: + break; + case TouchEvent.POINT_MOVE: + break; + case TouchEvent.PRIMARY_POINT_UP: + break; + case TouchEvent.NONE: + case TouchEvent.CANCEL: + break; + default: + break; + } + return false; + } +``` \ No newline at end of file diff --git a/graphics/BitmapShader.md b/graphics/BitmapShader.md index 8ac2302a829695831076f2988bc4f2f555ac2a6b..cddb612ec24e98ecf5e466ff8e102fbf03f758c9 100644 --- a/graphics/BitmapShader.md +++ b/graphics/BitmapShader.md @@ -1,5 +1,4 @@ - -# PixelMapShader +### **PixelMapShader** * openharmony API: ohos.agp.render.PixelMapShader * openharmony SDK版本:2.1.1.21 * IDE版本:2.1.0.501 diff --git a/graphics/Carema.md b/graphics/Carema.md new file mode 100644 index 0000000000000000000000000000000000000000..230e2c3698b69f049ff4e88663cfe0ff4d190388 --- /dev/null +++ b/graphics/Carema.md @@ -0,0 +1,118 @@ +### **Carema** +* openharmony API: ohos.agp.render.ThreeDimView +* openharmony SDK版本:2.1.1.21 +* IDE版本:2.1.0.501 +* 实现方案:3D效果转换 + +原库实现 +``` java +package com.example.rotatepicbrowserdemo; + +import android.view.animation.Animation; +import android.view.animation.Transformation; +import android.graphics.Camera; +import android.graphics.Matrix; + +/** + * An animation that rotates the view on the Y axis between two specified + * angles. This animation also adds a translation on the Z axis (depth) to + * improve the effect. + */ +public class Rotate3dAnimation extends Animation { + private final float mFromDegrees; + private final float mToDegrees; + private final float mCenterX; + private final float mCenterY; + private final float mDepthZ; + private final boolean mReverse; + private Camera mCamera; + + /** + * Creates a new 3D rotation on the Y axis. The rotation is defined by its + * start angle and its end angle. Both angles are in degrees. The rotation + * is performed around a center point on the 2D space, definied by a pair of + * X and Y coordinates, called centerX and centerY. When the animation + * starts, a translation on the Z axis (depth) is performed. The length of + * the translation can be specified, as well as whether the translation + * should be reversed in time. + * + * @param fromDegrees + * the start angle of the 3D rotation + * @param toDegrees + * the end angle of the 3D rotation + * @param centerX + * the X center of the 3D rotation + * @param centerY + * the Y center of the 3D rotation + * @param reverse + * true if the translation should be reversed, false otherwise + */ + public Rotate3dAnimation(float fromDegrees, float toDegrees, float centerX, float centerY, float depthZ, + boolean reverse) { + mFromDegrees = fromDegrees; + mToDegrees = toDegrees; + mCenterX = centerX; + mCenterY = centerY; + mDepthZ = depthZ; + mReverse = reverse; + } + + @Override + public void initialize(int width, int height, int parentWidth, int parentHeight) { + super.initialize(width, height, parentWidth, parentHeight); + mCamera = new Camera(); + } + + @Override + protected void applyTransformation(float interpolatedTime, Transformation t) { + final float fromDegrees = mFromDegrees; + float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime); + final float centerX = mCenterX; + final float centerY = mCenterY; + final Camera camera = mCamera; + final Matrix matrix = t.getMatrix(); + // 将当前的摄像头位置保存下来,以便变换进行完成后恢复成原位, + camera.save(); + // camera.translate,这个方法接受3个参数,分别是x,y,z三个轴的偏移量,我们这里只将z轴进行了偏移, + if (mReverse) { + // z的偏移会越来越大。这就会形成这样一个效果,view从近到远 + camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime); + } else { + // z的偏移会越来越小。这就会形成这样一个效果,我们的View从一个很远的地方向我们移过来,越来越近,最终移到了我们的窗口上面~ + camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime)); + } + + // 是给我们的View加上旋转效果,在移动的过程中,视图还会移Y轴为中心进行旋转。 + camera.rotateY(degrees); + // 是给我们的View加上旋转效果,在移动的过程中,视图还会移X轴为中心进行旋转。 + // camera.rotateX(degrees); + + // 这个是将我们刚才定义的一系列变换应用到变换矩阵上面,调用完这句之后,我们就可以将camera的位置恢复了,以便下一次再使用。 + camera.getMatrix(matrix); + // camera位置恢复 + camera.restore(); + + // 以View的中心点为旋转中心,如果不加这两句,就是以(0,0)点为旋转中心 + matrix.preTranslate(-centerX, -centerY); + matrix.postTranslate(centerX, centerY); + } +} +``` + +openharmony: +ThreeDimView 计算三维变换矩阵。变换矩阵可以在Canvas中使用,实现艺术字体、字符变形等效果。 +``` java + public void onDraw(Component component,Canvas canvas){ + Paint paint = new Paint(); + paint.setColor(Color.BLUE); + ThreeDimView threeDimView = new ThreeDimView(); + Matrix cameraMatrix = new Matrix(); + threeDimView.rotateY(100); + threeDimView.rotateX(100); + threeDimView.getMatrix(cameraMatrix); + cameraMatrix.preTranslate(-300,-300); + cameraMatrix.postTranslate(-300,-300); + canvas.concat(cameraMatrix); + canvas.drawRect(150,150,450,450,paint); + } +``` \ No newline at end of file diff --git a/graphics/LinearGradient.md b/graphics/LinearGradient.md new file mode 100644 index 0000000000000000000000000000000000000000..8f16b7f369e64ca619245049b5f5cff7cc8b1369 --- /dev/null +++ b/graphics/LinearGradient.md @@ -0,0 +1,43 @@ +### **LinearGradient** +* openharmony API: ohos.agp.render.LinearShader +* openharmony SDK版本:2.1.1.21 +* IDE版本:2.1.0.501 +* 实现方案: + +原三方库: + +linearGradient线性渐变,会用到Paint的setShader,Shader 被称为着色器,在opengl中这个概念经常被用到,android中的shader主要用来给图像着色,Shader在绘制过程中会返回横向重要的颜色组,Paint设置shader后,绘制时会从shader中获取颜色,也就是需要shader告诉画笔某处的颜色值。 + +``` java + mPaint = new Paint(); + mPaint.setColor(Color.BLUE); + mPaint.setAntiAlias(true); + mPaint.setStrokeWidth(3); + mPaint.setStyle(Paint.Style.FILL); + mPaint.setTextSize(20); + + LinearGradient linearGradient = new LinearGradient(getWidth(),400,0,0,Color.RED,Color.GREEN, Shader.TileMode.CLAMP); + mPaint.setShader(linearGradient); + canvas.drawRect(0,0,getWidth(),400,mPaint); +``` + +openharmony: + +线性渐变绘制 + +``` java + public void drawCircle(Component component, Canvas canvas){ + Paint paint = new Paint(); + float centerX = 450; + float centerY = 450; + int R = 150; + // 渐变颜色 + Color[] colors = {Color.YELLOW,Color.RED}; + // 渐变开始和结束坐标 + Point[] points = new Point[]{new Point(centerX,centerY + R),new Point(centerX,centerY - R)}; + LinearShader linearShader = new LinearShader(points,null,colors, Shader.TileMode.CLAMP_TILEMODE); + // 设置线性渐变 + paint.setShader(linearShader,Paint.ShaderType.LINEAR_SHADER); + canvas.drawCircle(centerX,centerY,R,paint); + } +``` \ No newline at end of file diff --git a/net/wifi/WifiManager.md b/net/wifi/WifiManager.md new file mode 100644 index 0000000000000000000000000000000000000000..8c65b9ad993fc968c18e9255fa38ee677ef2583a --- /dev/null +++ b/net/wifi/WifiManager.md @@ -0,0 +1,54 @@ +### **getScanResults** +>+ openharmony API:ohos.wifi.WifiDevice.getScanInfoList +>+ openharmony SDK版本:2.1.1.21 +>+ IDE版本:2.2.0.200 +>+ 实现方案: + +``` +public static List getWifiScanResults(Context context) { + // 获取WLAN管理对象 + WifiDevice wifiDevice = WifiDevice.getInstance(context); + // 调用获取WLAN开关状态接口,若WLAN打开,则返回true,否则返回false + boolean isWifiActive = wifiDevice.isWifiActive(); + if (!isWifiActive) { + return null; + } else { + // 调用WLAN扫描接口 + boolean isScanSuccess = wifiDevice.scan(); + if (isScanSuccess) { + // 调用获取扫描结果 + return wifiDevice.getScanInfoList(); + } else { + return null; + } + } +} +``` + +### **getConnectionInfo** +>+ openharmony API:ohos.wifi.WifiDevice.getLinkedInfo +>+ openharmony SDK版本:2.1.1.21 +>+ IDE版本:2.2.0.200 +>+ 实现方案: + +``` +public static Optional getWifiConnectionInfo(Context context) { + // 获取WLAN管理对象 + WifiDevice wifiDevice = WifiDevice.getInstance(context); + // 调用WLAN连接状态接口,确定当前设备是否连接WLAN + boolean isConnected = wifiDevice.isConnected(); + if (isConnected) { + // 获取WLAN连接信息 + return wifiDevice.getLinkedInfo(); + // Optional linkedInfo = wifiDevice.getLinkedInfo(); + // // 获取连接信息中的SSID + // String ssid = linkedInfo.get().getSsid(); + // // 获取WLAN的IP信息 + // Optional ipInfo = wifiDevice.getIpInfo(); + // // 获取IP信息中的IP地址与网关 + // int ipAddress = ipInfo.get().getIpAddress(); + // int gateway = ipInfo.get().getGateway(); + } + return null; +} +``` \ No newline at end of file diff --git a/speech/tts/TextToSpeech.md b/speech/tts/TextToSpeech.md new file mode 100644 index 0000000000000000000000000000000000000000..5489b567b735f21d4268140df84fc2ef3c19f209 --- /dev/null +++ b/speech/tts/TextToSpeech.md @@ -0,0 +1,55 @@ +### **TextToSpeech** +* openharmony API: ohos.ai.tts.TtsClient +* openharmony SDK版本:2.2.0.1 +* IDE版本:2.2.Beta1 +* 实现方案: + +原三方库: +原项目使用 谷歌 tts服务进行文字转语音。 +使用方法: + +``` java +Activity 实现 TextToSpeech.OnInitListener 并初始化onInit()方法 +TextToSpeech mTextToSpeech = new TextToSpeech(Context context, OnInitListener listener); +// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规 + mTextToSpeech.setPitch(1.0f); + // 设置语速 + mTextToSpeech.setSpeechRate(0.5f); + TextToSpeech的speak方法有两个重载。 +// 执行朗读的方法 +speak(CharSequence text,int queueMode,Bundle params,String utteranceId); +//第二个参数queueMode用于指定发音队列模式,两种模式选择 + //(1)TextToSpeech.QUEUE_FLUSH:该模式下在有新任务时候会清除当前语音任务,执行新的语音任务 + //(2)TextToSpeech.QUEUE_ADD:该模式下会把新的语音任务放到语音任务之后, + +mTextToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null); +使用完成销毁 + if (mTextToSpeech != null) { + mTextToSpeech.stop(); + mTextToSpeech.shutdown(); + } +``` + +openharmony: + +鸿蒙是基于华为智慧引擎的语音播报引擎 +使用方法: + +``` java + 1.初始化TTsClient + TtsClient.getInstance().create(this, TtsListener ttsListener); + 2.在TtsListener 监听中的 onEvent 方法中 + private TtsListener ttsListener = new TtsListener() { + public void onEvent(int var1, PacMap var2) { + if (var1 == 0) { + TtsParams var3 = new TtsParams(); + var3.setDeviceId(UUID.randomUUID().toString()); + TtsClient.getInstance().init(var3); + } + } + 3.if (initResult) { + TtsClient.getInstance().speakText("欢迎使用语音播报!", null); + } + 4.使用完成后销毁TTS客户端 + TtsClient.getInstance().destroy(); +``` \ No newline at end of file diff --git a/view/WindowManager.md b/view/WindowManager.md index a1a5d8867c501fd6b876a32278f64f8bd29e801d..e777e1fc2627faa44541c499c5fa6c996c4e2a0b 100644 --- a/view/WindowManager.md +++ b/view/WindowManager.md @@ -1,4 +1,5 @@ -### **addView** +### 方案一 +### **addView** >+ openharmony API: ohos.agp.window.dialog.PopupDialog.show >+ openharmony SDK版本:2.1.0.17 >+ IDE版本:2.1.0.141 @@ -23,4 +24,91 @@ popupDialog.setBackColor(new Color(0x00000000)); popupDialog.show(); ``` ->+ 补充说明:当前SDK版本:2.1.0.17不生效,后续支持。 \ No newline at end of file +>+ 补充说明:当前SDK版本:2.1.0.17不生效,后续支持。 + + +### 方案二 +### **addView** +>+ openharmony API: ohos.agp.window.service.WindowManager +>+ openharmony SDK版本:2.1.0.17 +>+ IDE版本:2.2.0.200 +>+ 实现方案:自行实现,实现方法如下 + +``` java +WindowManager windowManager = WindowManager.getInstance(); +/** + * component 需要加载的布局自定义Component + * context 当前上下文 + * typeFlag 窗口的类型 例如:WindowManager.LayoutConfig.MOD_APPLICATION_OVERLAY + */ +Window window = WindowManager.getInstance().addComponent(ComponentContainer component, Context context, int typeFlag); +//窗口是否可以拖动 +window.setMovable(true); +//设置是否启用透明度。 +window.setTransparent(true); +WindowManager.LayoutConfig layoutConfig1 = new WindowManager.LayoutConfig(); +//表示窗口的宽度。 +layoutConfig1.width = 100; +//表示窗口的高度。 +layoutConfig1.height = 100; +//对其方式 +layoutConfig1.alignment = LayoutAlignment.CENTER; +//指示窗口的各种标志。 +layoutConfig1.flags = WindowManager.LayoutConfig.INPUT_ADJUST_PAN; +// 表示窗口的 X 坐标。 +layoutConfig1.x = 0; +// 表示窗口的 y 坐标。 +layoutConfig1.y = 0; +//设置移动窗口的边界。 +window.setBoundRect(Rect rect); +window.setLayoutConfig(layoutConfig1); + +//关闭当前window +windowManager.destroyWindow(window); +``` + +### 方案三 +### **WindowManager** +>+ openharmony API: ohos.agp.window.service.WindowManager +>+ openharmony SDK版本:2.1.0.17 +>+ IDE版本:2.2.0.200 +>+ 实现方案:自行实现,实现方法如下 + +原三方库: +android.view.WindowManager.LayoutParams 隐藏与显示标题栏与状态栏 + +**设置状态栏** +``` java +getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) //显示状态栏 +getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) //隐藏状态栏 +``` +**设置标题栏** +``` java +mActivity.getActionBar().show()//显示标题栏 +mActivity.getActionBar().hide()//隐藏标题栏 +``` + +openharmony: +隐藏与显示标题栏与状态栏 +**设置状态栏** +``` java +//显示状态栏 +WindowManager.getInstance().getTopWindow().get().setStatusBarVisibility(Component.VISIBLE) +//隐藏状态栏 +WindowManager.getInstance().getTopWindow().get().setStatusBarVisibility(Component.INVISIBLE) +``` +**设置标题栏** + 因openharmony未找到获取标题栏的方法,隐藏系统的标题栏,需要在config中进行配置 +``` java +// 隐藏标题栏 +"metaData": { + "customizeData": [ + { + "name": "hwc-theme", + "value": "androidhwext:style/Theme.Emui.NoTitleBar", + "extra": "" + } + ] +} +``` + diff --git a/view/inputmethod/InputMethodManager.md b/view/inputmethod/InputMethodManager.md new file mode 100644 index 0000000000000000000000000000000000000000..1acf30b154befd45af93116539bdfd4fc306aba5 --- /dev/null +++ b/view/inputmethod/InputMethodManager.md @@ -0,0 +1,41 @@ +### **showSoftInput** +>+ openharmony API:ohos.miscservices.inputmethod.InputMethodController +>+ openharmony SDK版本:2.1.1.21 +>+ IDE版本:2.2.0.200 +>+ 实现方案: + +``` +public static boolean showSoftInput() { + try { + Class inputClass = Class.forName("ohos.miscservices.inputmethod.InputMethodController"); + Method method = inputClass.getMethod("getInstance"); + Object object = method.invoke(new Object[]{}); + Method startInput = inputClass.getMethod("startInput", int.class, boolean.class); + return (boolean) startInput.invoke(object, 1, true); + } catch (Exception e) { + e.printStackTrace(); + } + return false; +} +``` + +### **hideSoftInputFromWindow** +>+ openharmony API:ohos.miscservices.inputmethod.InputMethodController +>+ openharmony SDK版本:2.1.1.21 +>+ IDE版本:2.2.0.2001 +>+ 实现方案: + +``` +public static boolean hideSoftInput() { + try { + Class inputClass = Class.forName("ohos.miscservices.inputmethod.InputMethodController"); + Method method = inputClass.getMethod("getInstance"); + Object object = method.invoke(new Object[]{}); + Method stopInput = inputClass.getMethod("stopInput", int.class); + return (boolean) stopInput.invoke(object, 1); + } catch (Exception e) { + e.printStackTrace(); + } + return false; +} +``` \ No newline at end of file diff --git a/widget/LinearLayout.md b/widget/LinearLayout.md new file mode 100644 index 0000000000000000000000000000000000000000..a6eb0f5b28f8cf7c1335e6905445893781877045 --- /dev/null +++ b/widget/LinearLayout.md @@ -0,0 +1,110 @@ +### **LinearLayout** +>+ openharmony API:`ohos.agp.components.DirectionalLayout` +>+ openharmony SDK版本:2.1.1.21以上 +>+ IDE版本:2.2.0.200 +>+ 实现方案: + +* 原库实现 + +```xml + + + +``` + +```java + LinearLayout linearLayout = new LinearLayout(context); + linearLayout.setId(View.generateViewId()); + linearLayout.setOrientation(LinearLayout.VERTICAL); + linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); +``` + +* 替换实现 + +```xml + + + +``` +```java + DirectionalLayout directionalLayout = new DirectionalLayout(context); + directionalLayout.setId(component.getId()); + directionalLayout.setOrientation(DirectionalLayout.VERTICAL); + DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig( + DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT); + directionalLayout.setLayoutConfig(layoutConfig); +``` + +#### **setOrientation** +>+ openharmony API:`ohos.agp.components.DirectionalLayout.setOrientation` +>+ openharmony SDK版本:2.1.1.21以上 +>+ IDE版本:2.2.0.200 +>+ 实现方案:DirectionalLayout.setOrientation + + +#### **getOrientation** +>+ openharmony API:`ohos.agp.components.DirectionalLayout.getOrientation` +>+ openharmony SDK版本:2.1.1.21以上 +>+ IDE版本:2.2.0.200 +>+ 实现方案:DirectionalLayout.getOrientation + + +#### **setGravity** +>+ openharmony API:`ohos.agp.components.DirectionalLayout.setAlignment` +>+ openharmony SDK版本:2.1.1.21以上 +>+ IDE版本:2.2.0.200 +>+ 实现方案:DirectionalLayout.setAlignment + + +#### **getGravity** + +>+ openharmony API:`ohos.agp.components.DirectionalLayout.getAlignment` +>+ openharmony SDK版本:2.1.1.21以上 +>+ IDE版本:2.2.0.200 +>+ 实现方案:DirectionalLayout.getAlignment + + +#### **setWeightSum** +>+ openharmony API:`ohos.agp.components.DirectionalLayout.setTotalWeight` +>+ openharmony SDK版本:2.1.1.21以上 +>+ IDE版本:2.2.0.200 +>+ 实现方案:DirectionalLayout.setTotalWeight + + +#### **getWeightSum** +>+ openharmony API:`ohos.agp.components.DirectionalLayout.getTotalWeight` +>+ openharmony SDK版本:2.1.1.21以上 +>+ IDE版本:2.2.0.200 +>+ 实现方案:DirectionalLayout.getTotalWeight diff --git a/widget/TextSwitcher.md b/widget/TextSwitcher.md new file mode 100644 index 0000000000000000000000000000000000000000..39d374da589e28a4f6dff1d8ffd94f5f01ba4d03 --- /dev/null +++ b/widget/TextSwitcher.md @@ -0,0 +1,77 @@ +### TextSwitcher +* openharmony API: ohos.agp.components.PageFlipper +* openharmony SDK版本:2.2.0.1 +* IDE版本:2.2.0.200 +* 实现方案: + + +**原三方库:** + +`android.widget.ViewSwitcher`仅包含类型为 的子项的特殊化`android.widget.TextView`。TextSwitcher 可用于为屏幕上的标签设置动画。每当`setText(java.lang.CharSequence)`被调用时,TextSwitcher 都会将当前文本动画化,并将新文本动画化。 + +``` java + textSwitcher = new TextSwitcher(context); + textSwitcher.addView(createViewForTextSwitcher(context)); + textSwitcher.addView(createViewForTextSwitcher(context)); +``` +``` java +public void update(int newPosition, int oldPosition, int totalElements) { + textView.setText(" / " + totalElements); + int offset = (int) (textSwitcher.getHeight() * 0.75); + int duration = 250; + if (newPosition > oldPosition) { + textSwitcher.setInAnimation(createPositionAnimation(-offset, 0, 0f, 1f, duration)); + textSwitcher.setOutAnimation(createPositionAnimation(0, offset, 1f, 0f, duration)); + } else if (oldPosition > newPosition) { + textSwitcher.setInAnimation(createPositionAnimation(offset, 0, 0f, 1f, duration)); + textSwitcher.setOutAnimation(createPositionAnimation(0, -offset, 1f, 0f, duration)); + } + textSwitcher.setText(String.valueOf(newPosition + 1)); +} +``` + +``` java +/** + * Sets the text of the next view and switches to the next view. This can + * be used to animate the old text out and animate the next text in. + * + * @param text the new text to display + */ +public void setText(CharSequence text) { + final TextView t = (TextView) getNextView(); + t.setText(text); + showNext(); +} +``` + + +**openharmony:** + +当PageFlipper在切换过程中为两个或多个子组件设置动画时,页面翻转器提供翻转效果。它还可以在预定的时间段内自动翻阅添加的子组件。 +**PageFlipper中调用setText之前需要先执行showNext进行切换,再获取当前Text组件后进行赋值** +``` java + private PageFlipper textSwitcher; + ... + textSwitcher = new PageFlipper(context); + textSwitcher.addComponent(createViewForTextSwitcher(context)); + textSwitcher.addComponent(createViewForTextSwitcher(context)); + addComponent(textSwitcher, new LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, + ComponentContainer.LayoutConfig.MATCH_CONTENT)); +``` + +``` java +public void update(int newPosition, int oldPosition, int totalElements) { + textView.setText(" / " + totalElements); + int offset = (int)(textSwitcher.getHeight()*0.75f); + if (newPosition > oldPosition) { + textSwitcher.setIncomingAnimation(createPositionAnimation(-offset, 0, 0f, 1f, DURATION)); + textSwitcher.setOutgoingAnimation(createPositionAnimation(0, offset, 1f, 0f, DURATION)); + } else if (oldPosition > newPosition) { + textSwitcher.setIncomingAnimation(createPositionAnimation(offset, 0, 0f, 1f, DURATION)); + textSwitcher.setOutgoingAnimation(createPositionAnimation(0, -offset, 1f, 0f, DURATION)); + } + textSwitcher.showNext(); + Text text = (Text) textSwitcher.getCurrentComponent(); + text.setText(String.valueOf(newPosition + 1)); +} +``` \ No newline at end of file diff --git a/widget/Toast.md b/widget/Toast.md new file mode 100644 index 0000000000000000000000000000000000000000..9e407d4a559d76b2f0fc0e836643fa3cfadd4d96 --- /dev/null +++ b/widget/Toast.md @@ -0,0 +1,52 @@ +### Toast +* openharmony API:ohos.agp.window.dialog.ToastDialog +* openharmony SDK版本:2.2.0.1 +* IDE版本:2.2.0.200 +* 实现方案: + + +原库显示一个Toast提示为: +``` java + Toast.makeText(this, "测试",Toast.LENGTH_SHORT); +``` + +openharmony系统中通过ToastDialog实现与原库显示效果类似: +``` java + public static void showCustomText(Context context, String toastStr) { + Text textComponent = new Text(context); + textComponent.setText(toastStr); + + //设置间距为10vp + textComponent.setPadding(60, 40, 60, 40); + textComponent.setTextColor(Color.WHITE); + textComponent.setWidth(500); + textComponent.setTextAlignment(TextAlignment.CENTER); + textComponent.setTextSize(40); + + ShapeElement shapeElement = new ShapeElement(); + shapeElement.setShape(ShapeElement.RECTANGLE); + shapeElement.setCornerRadius(50); + //设置背景半透明 + shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.argb(180, 11, 11, 11))); + textComponent.setBackground(shapeElement); + //设置文字允许多行 + textComponent.setMultipleLine(true); + + DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(); + layoutConfig.width = ComponentContainer.LayoutConfig.MATCH_CONTENT; + layoutConfig.height = ComponentContainer.LayoutConfig.MATCH_CONTENT; + layoutConfig.alignment = LayoutAlignment.CENTER; + layoutConfig.setMarginBottom(220); + textComponent.setLayoutConfig(layoutConfig); + + ToastDialog toastDialog = new ToastDialog(context).setDuration(500).setComponent(textComponent).setAlignment(LayoutAlignment.BOTTOM); + //设置弹框背景透明 + toastDialog.setTransparent(true); + toastDialog.show(); + } +``` + +调用方式 +```java +Utils.showCustomText(getContext(), "测试"); +``` \ No newline at end of file