package com.tdtxdcxm.mymathdraw.slice;
import com.tdtxdcxm.mymathdraw.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import parsii.eval.Expression;
import parsii.eval.Parser;
import parsii.eval.Scope;
import parsii.eval.Variable;
import parsii.tokenizer.ParseException;
import java.util.ArrayList;
public class MainAbilitySlice extends AbilitySlice {
DirectionalLayout rootdl_stkl_ddl2,rootdl_stkl_ddl2_set;
Button topdl_but;
TextField bottomdl_tfd;
Text topdl_txt,paintswitch_text,uiswitch_text;
Switch switch_paintcolor,switch_uicolor;
Paint bluepaint = new Paint();
Paint purplepaint = new Paint();
Paint paint = bluepaint;//用于绘制任务的默认画笔(蓝色)
int origin_x,origin_y;
boolean isgeneratexy = false;
public void initPaint(){
bluepaint.setColor(Color.BLUE);
bluepaint.setStrokeWidth(8);
purplepaint.setColor(new Color(Color.rgb(160, 32, 240)));
purplepaint.setStrokeWidth(8);
}
public void initMASComponents(){
topdl_txt = (Text) findComponentById(ResourceTable.Id_topdl_txt);
topdl_but = (Button) findComponentById(ResourceTable.Id_topdl_but);
bottomdl_tfd = (TextField) findComponentById(ResourceTable.Id_bottomdl_tfd);
rootdl_stkl_ddl2 = (DirectionalLayout) findComponentById(ResourceTable.Id_rootdl_stkl_ddl2);
rootdl_stkl_ddl2_set = (DirectionalLayout) findComponentById(ResourceTable.Id_rootdl_stkl_ddl2_set);
paintswitch_text = (Text) findComponentById(ResourceTable.Id_paintswitch_text);
uiswitch_text = (Text) findComponentById(ResourceTable.Id_uiswitch_text);
switch_paintcolor = (Switch) findComponentById(ResourceTable.Id_switch_paintcolor);
switch_uicolor = (Switch) findComponentById(ResourceTable.Id_switch_uicolor);
bottomdl_tfd.setHint("输入函数>>>"+"\n【右上角表情】:\n1.单击—绘制"+"\n2.长按-清空");
topdl_txt.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
bottomdl_tfd.clearFocus();
rootdl_stkl_ddl2.setVisibility(Component.VISIBLE);
if(topdl_but.getText().equals("")){
topdl_but.setText("");
}
}
});
topdl_but.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
bottomdl_tfd.clearFocus();
String function = bottomdl_tfd.getText();
ArrayList<float[]> xylist = generateXY(function);//根据表达式生成许多x、y点坐标
if(xylist == null){
return;
}
bottomdl_tfd.addDrawTask(new Component.DrawTask() {
@Override
public void onDraw(Component component, Canvas canvas) {
if(isgeneratexy) {
for (float[] xyfloats : xylist) {
canvas.drawPoint(xyfloats[0], xyfloats[1], paint);
}
}
}
}, Component.DrawTask.BETWEEN_BACKGROUND_AND_CONTENT);
}
});
topdl_but.setLongClickedListener(new Component.LongClickedListener() {
@Override
public void onLongClicked(Component component) {
bottomdl_tfd.setText("");
bottomdl_tfd.clearFocus();
bottomdl_tfd.addDrawTask(new Component.DrawTask() {
@Override
public void onDraw(Component component, Canvas canvas) {
//用于清空,画一个空白
}
}, Component.DrawTask.BETWEEN_BACKGROUND_AND_CONTENT);
}
});
rootdl_stkl_ddl2.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
//弹出设置面板后,单击其余区域会退出(收回)设置状态
rootdl_stkl_ddl2.setVisibility(Component.HIDE);
topdl_but.setText("");
}
});
rootdl_stkl_ddl2_set.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
//这里用来“吸收”单击事件,否则当单击设置面板时,也会退出设置状态
}
});
switch_paintcolor.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
@Override
public void onCheckedChanged(AbsButton absButton, boolean b) {
if(b == true){
paint = purplepaint;//把紫色画笔给绘制任务画笔
paintswitch_text.setText("画笔:紫色");
paintswitch_text.setTextColor(new Color(Color.rgb(160, 32, 240)));
}
else{
paint = bluepaint;//把蓝色画笔给绘制任务画笔,还原默认
paintswitch_text.setText("画笔:蓝色");
paintswitch_text.setTextColor(Color.BLUE);
}
}
});
switch_uicolor.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
@Override
public void onCheckedChanged(AbsButton absButton, boolean b) {
if(b == true){
uiswitch_text.setText("界面:黑暗");
uiswitch_text.setTextColor(new Color(Color.rgb(54, 54, 54)));
ShapeElement shape = new ShapeElement();
shape.setRgbColor(new RgbColor(105,105,105));
bottomdl_tfd.setBackground(shape);
}
else{
uiswitch_text.setText("界面:明亮");
uiswitch_text.setTextColor(new Color(Color.rgb(238, 99, 99)));
ShapeElement shape = new ShapeElement();
shape.setRgbColor(new RgbColor(255,255,255));
bottomdl_tfd.setBackground(shape);
}
}
});
}
public ArrayList<float[]> generateXY(String function){
if(function.equals("")){
return null;
}
origin_x = bottomdl_tfd.getWidth() / 2;//将绘图坐标远点的x设定在此
origin_y = bottomdl_tfd.getHeight() / 2;//将绘图坐标远点的y设定在此
System.out.println(origin_x);
System.out.println(origin_y);
ArrayList<float[]> xylist = new ArrayList<>();
StringBuilder stringBuilder = new StringBuilder(function);
Scope scope = new Scope();
Variable var_x = scope.getVariable("x");
Expression expression = null;
try {
expression = Parser.parse(function, scope);
} catch (ParseException e) {
e.printStackTrace();
}
if(expression == null){
return null;
}
for(float x = -400f;x <= 400.0f;x = x + 0.001f){
var_x.setValue((double) x);
double y = expression.evaluate();
//将图形默认放大
float[] xyfloats = {x*140+origin_x,-140*((float) y)+origin_y};
xylist.add(xyfloats);
}
isgeneratexy = true;
return xylist;
}
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
initPaint();
initMASComponents();
}
@Override
public void onInactive() {
super.onInactive();
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
@Override
public void onBackground() {
super.onBackground();
}
@Override
public void onStop() {
super.onStop();
}
}
package com.tdtxdcxm.mymathdraw;
import com.tdtxdcxm.mymathdraw.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
public class MainAbility extends Ability {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(MainAbilitySlice.class.getName());
}
}
package com.tdtxdcxm.mymathdraw;
import ohos.aafwk.ability.AbilityPackage;
public class MyApplication extends AbilityPackage {
@Override
public void onInitialize() {
super.onInitialize();
}
}
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:shape="rectangle">
<corners
ohos:radius="20vp"/>
<solid
ohos:color="#B2F1EDED"/>
shape>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:shape="rectangle">
<solid
ohos:color="#FFFFFF"/>
shape>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:id="$+id:mymathdraw_rootdl"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="vertical">
<StackLayout
ohos:id="$+id:rootdl_stkl"
ohos:height="match_parent"
ohos:width="match_parent">
<DirectionalLayout
ohos:id="$+id:rootdl_stkl_ddl1"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="vertical"
ohos:background_element="white">
<DirectionalLayout
ohos:id="$+id:rootdl_stkl_ddl1_topdl"
ohos:height="0"
ohos:weight="0.7"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="horizontal"
ohos:background_element="#70B3ECF3">
<Text
ohos:id="$+id:topdl_txt"
ohos:height="match_parent"
ohos:width="0"
ohos:weight="8"
ohos:text="MathPad"
ohos:text_size="35vp"
ohos:text_color="#FF2DDE53"
ohos:text_alignment="vertical_center"
>
Text>
<Button
ohos:id="$+id:topdl_but"
ohos:height="match_parent"
ohos:width="0"
ohos:weight="2"
ohos:text=""
ohos:text_size="30vp"
ohos:text_alignment="center"
ohos:background_element="#9FA9EFE5">
Button>
DirectionalLayout>
<DirectionalLayout
ohos:id="$+id:rootdl_stkl_ddl1_bottomdl"
ohos:height="0"
ohos:weight="9.3"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="vertical"
ohos:background_element="#FFFFFFFF">
<TextField
ohos:id="$+id:bottomdl_tfd"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:hint="输入函数>>>"
ohos:text_size="25vp"
ohos:text_alignment="start"
ohos:background_element="#FFFFFFFF">
TextField>
DirectionalLayout>
DirectionalLayout>
<DirectionalLayout
ohos:id="$+id:rootdl_stkl_ddl2"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:visibility="hide"
ohos:alignment="bottom"
ohos:orientation="vertical"
ohos:background_element="#00FFFFFF">
<DirectionalLayout
ohos:id="$+id:rootdl_stkl_ddl2_set"
ohos:height="220vp"
ohos:width="300vp"
ohos:bottom_margin="20vp"
ohos:layout_alignment="center"
ohos:background_element="$graphic:background_ability_ddl2_set"
ohos:orientation="vertical"
ohos:alignment="center">
<DirectionalLayout
ohos:height="0"
ohos:weight="1"
ohos:width="match_parent"
ohos:alignment="bottom"
ohos:orientation="horizontal">
<Text
ohos:id="$+id:paintswitch_text"
ohos:height="30vp"
ohos:width="0"
ohos:weight="1"
ohos:text_alignment="center"
ohos:auto_font_size="true"
ohos:text="画笔:蓝色"
ohos:text_color="blue"
>
Text>
<Text
ohos:id="$+id:uiswitch_text"
ohos:height="30vp"
ohos:width="0"
ohos:weight="1"
ohos:text_alignment="center"
ohos:auto_font_size="true"
ohos:text="界面:明亮"
ohos:text_color="#FFEE6363"
>
Text>
DirectionalLayout>
<DirectionalLayout
ohos:height="0"
ohos:weight="1"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="horizontal">
<Switch
ohos:id="$+id:switch_paintcolor"
ohos:height="25vp"
ohos:width="90vp"
ohos:right_margin="30vp"
ohos:auto_font_size="true"
ohos:text_state_on="紫色"
ohos:text_color_on="red"
ohos:text_state_off="蓝色"
ohos:text_color_off="black"
>
Switch>
<Switch
ohos:id="$+id:switch_uicolor"
ohos:height="25vp"
ohos:width="90vp"
ohos:left_margin="30vp"
ohos:auto_font_size="true"
ohos:text_state_on="黑暗"
ohos:text_color_on="red"
ohos:text_state_off="明亮"
ohos:text_color_off="black"
>
Switch>
DirectionalLayout>
DirectionalLayout>
DirectionalLayout>
StackLayout>
DirectionalLayout>
apply plugin: 'com.huawei.ohos.hap'
apply plugin: 'com.huawei.ohos.decctest'
//For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#section1112183053510
ohos {
compileSdkVersion 6
defaultConfig {
compatibleSdkVersion 6
}
buildTypes {
release {
proguardOpt {
proguardEnabled false
rulesFiles 'proguard-rules.pro'
}
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.har'])
testImplementation 'junit:junit:4.13.1'
ohosTestImplementation 'com.huawei.ohos.testkit:runner:2.0.0.200'
implementation 'com.scireum:parsii:4.0'
}
decc {
supportType = ['html', 'xml']
}
基于Canvas的MyMathDraw[我的数学板][底部弹窗][API V6]