用JAVASCRIPT寫(xiě)的并不是JAVA。

創(chuàng)新互聯(lián)公司-專(zhuān)業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比余干網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式余干網(wǎng)站制作公司更省心,省錢(qián),快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋余干地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴(lài)。
javascript是一種腳本語(yǔ)言,是基于瀏覽器解析的,可以不用編譯,也就是說(shuō)只要寫(xiě)了一段代碼就能立即在瀏覽器中運(yùn)行!
原理是:
1 用JS操作html標(biāo)簽,也就是找到img標(biāo)簽,它代表的是一個(gè)圖片
2 用JS來(lái)操作它在坐標(biāo),讓它位移,
這樣就產(chǎn)生了轉(zhuǎn)動(dòng)的效果
like this :
html
head
title/title
script type="text/javascript"
function rotateImage() {
imageToRotate = document.getElementById('imgRotate');
imageToRotate.style.filter= "progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand')";
window.setTimeout("rotate()",100);
}
var imageToRotate;
var degreeToRotate=0;
function rotate(){
var deg2radians = Math.PI * 2 / 360;
degreeToRotate++;
degreeToRotate=degreeToRotate%360;
rad = degreeToRotate * deg2radians ;
costheta = Math.cos(rad);
sintheta = Math.sin(rad);
imageToRotate.filters.item(0).M11 = costheta;
imageToRotate.filters.item(0).M12 = -sintheta;
imageToRotate.filters.item(0).M21 = sintheta;
imageToRotate.filters.item(0).M22 = costheta;
window.setTimeout("rotate()",100);
}
/script
/head
body onload="rotateImage();"
br /
canvas id="canvas" width="800" height="600"
img id="imgRotate" src="" /
/canvas
/body
/html
把代碼放到文本中,然后保存為html,點(diǎn)擊后就可以看到效果了
以華為Mate40手機(jī)為例:
升級(jí)HarmonyOS系統(tǒng)后,從屏幕右側(cè)頂部下滑出控制中心界面,點(diǎn)擊自動(dòng)旋轉(zhuǎn)開(kāi)啟屏幕自動(dòng)旋轉(zhuǎn)功能。
程序?qū)崿F(xiàn)思路: 在javafx中Node對(duì)象有一個(gè)effect屬性,可以用于實(shí)現(xiàn)各種特效。PerspectiveTransform特效可以使Node對(duì)象實(shí)現(xiàn)透視變換。因此我們可以通過(guò)計(jì)算透視變換中每個(gè)點(diǎn)的位置來(lái)實(shí)現(xiàn)3D翻轉(zhuǎn)特效。
實(shí)現(xiàn)步驟: 1、定義FlipView對(duì)象。包含以下屬性:
復(fù)制代碼 代碼如下:
//正面視圖
public Node frontNode;
//反面視圖
public Node backNode;
//是否翻轉(zhuǎn)
boolean flipped = false;
//翻轉(zhuǎn)角度
DoubleProperty time = new SimpleDoubleProperty(Math.PI / 2);
//正面翻轉(zhuǎn)特效
PerspectiveTransform frontEffect = new PerspectiveTransform();
//反面翻轉(zhuǎn)特效
PerspectiveTransform backEffect = new PerspectiveTransform();
create方法返回需要顯示的內(nèi)容:
復(fù)制代碼 代碼如下:
private void create() {
time.addListener(new ChangeListener() {
@Override
public void changed(ObservableValue? extends Number arg0,
Number arg1, Number arg2) {
setPT(frontEffect, time.get());
setPT(backEffect, time.get());
}
});
anim.getKeyFrames().addAll(frame1, frame2);
backNode.visibleProperty().bind(
Bindings.when(time.lessThan(0)).then(true).otherwise(false));
frontNode.visibleProperty().bind(
Bindings.when(time.lessThan(0)).then(false).otherwise(true));
setPT(frontEffect, time.get());
setPT(backEffect, time.get());
frontNode.setEffect(frontEffect);
backNode.setEffect(backEffect);
getChildren().addAll(backNode, frontNode);
}
以上代碼需要注意的是: 隨著time值的變化frontEffect和backEffect的值也會(huì)隨著變換。 2、PerspectiveTransform特效的實(shí)現(xiàn)使用了Math.sin()和Math.cos()方法模擬3D角度變換。 具體實(shí)現(xiàn)如下:
復(fù)制代碼 代碼如下:
private void setPT(PerspectiveTransform pt, double t) {
double width = 200;
double height = 200;
double radius = width / 2;
double back = height / 10;
pt.setUlx(radius - Math.sin(t) * radius);
pt.setUly(0 - Math.cos(t) * back);
pt.setUrx(radius + Math.sin(t) * radius);
pt.setUry(0 + Math.cos(t) * back);
pt.setLrx(radius + Math.sin(t) * radius);
pt.setLry(height - Math.cos(t) * back);
pt.setLlx(radius - Math.sin(t) * radius);
pt.setLly(height + Math.cos(t) * back);
}
3、角度變換在1秒的時(shí)間內(nèi)從3.14/2變換到-3.14/2。
復(fù)制代碼 代碼如下:
KeyFrame frame1 = new KeyFrame(Duration.ZERO, new KeyValue(time,
Math.PI / 2, Interpolator.LINEAR));
KeyFrame frame2 = new KeyFrame(Duration.seconds(1),
new EventHandler() {
@Override
public void handle(ActionEvent event) {
flipped = !flipped;
}
}, new KeyValue(time, -Math.PI / 2, Interpolator.LINEAR));
4、FlipView對(duì)象的創(chuàng)建:通過(guò)構(gòu)造函數(shù)可以很方便的創(chuàng)建FlipView對(duì)象.
復(fù)制代碼 代碼如下:
ImageView image1 = new ImageView(new Image(getClass()
.getResourceAsStream("lion1.png")));
ImageView image2 = new ImageView(new Image(getClass()
.getResourceAsStream("lion2.png")));
FlipView flip = new FlipView(image1, image2);
應(yīng)用程序怎么樣設(shè)置可以讓自己隨著設(shè)備的傾斜度變化而旋轉(zhuǎn)方向呢?在AndroidManifest.xml文件中的android:screenOrientation就可以了。這里追蹤一下它的內(nèi)部機(jī)制。
先看一個(gè)最關(guān)鍵的部件:/frameworks/base/core/java/android/view/WindowOrientationListener.java
這個(gè)接口注冊(cè)一個(gè)accelerator,并負(fù)責(zé)把a(bǔ)ccelerator的數(shù)據(jù)轉(zhuǎn)化為orientation。這個(gè)API對(duì)應(yīng)用程序不公開(kāi),我看Android2.3的源碼時(shí)發(fā)現(xiàn)只有PhoneWindowManager使用到它了。
/frameworks/base/policy/../PhoneWindowManager.java
PhonwWindowManager注冊(cè)了一個(gè)WindowOrientationListener,就可以異步獲取當(dāng)前設(shè)備的orientation了。再結(jié)合應(yīng)用程序在AndroidManifest.xml中設(shè)置的值來(lái)管理著應(yīng)用程序界面的旋轉(zhuǎn)方向。以下是PhoneWindowManager.java中相關(guān)的兩個(gè)代碼片段。
java代碼:
復(fù)制代碼
讓?xiě)?yīng)用程序隨屏幕方向自動(dòng)旋轉(zhuǎn)的實(shí)現(xiàn)原理就這么交待完了。我解決這一步時(shí)也沒(méi)有費(fèi)多少力氣,在板子上打開(kāi)SensorTest,對(duì)比一下XYZ三個(gè)軸和MileStone上面的數(shù)據(jù),修改一下正負(fù)值就可以了。但要解決Teeter運(yùn)行時(shí)Z軸反轉(zhuǎn)的問(wèn)題,還得深層次挖一挖。
PhoneWindowManager.java中有這么一句:
mWindowManager.setRotation(rotation, false, mFancyRotationAnimation);
當(dāng)PhonewindowManager通過(guò)WindowOrientationListener這個(gè)監(jiān)聽(tīng)器得知屏幕方向改變時(shí),會(huì)通知給WindowManagerService(/frameworks/base/service/../WindowManagerService.java)
WindowManagerService中有這么一個(gè)監(jiān)聽(tīng)器集合:mRotationWatchers,誰(shuí)想監(jiān)聽(tīng)屏幕方向改變,就會(huì)在這里注冊(cè)一個(gè)監(jiān)聽(tīng)器。SensorManager就這么干了。然后,通過(guò)下面這個(gè)異步方法獲知當(dāng)前的屏幕方向
java代碼:
public void onRotationChanged(int rotation) {
synchronized(sListeners) {
sRotation = rotation;
}
}
static int getRotation() {
synchronized(sListeners) {
return sRotation;
}
}
文章標(biāo)題:屏幕自動(dòng)旋轉(zhuǎn)java代碼,屏幕自動(dòng)旋轉(zhuǎn)java代碼是什么
網(wǎng)址分享:http://chinadenli.net/article34/dsgejpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、搜索引擎優(yōu)化、品牌網(wǎng)站建設(shè)、定制網(wǎng)站、ChatGPT、定制開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)