android旋轉view
A. 求助,怎麼用android實現控制項的3D立體旋轉效果
實現水平滑動,所以可在手勢抬起的時候進行判斷並處理,是滑動顯得流暢,代碼如下:
java">packagecom.example.rotation3dview;
importandroid.content.Context;
importandroid.graphics.Camera;
importandroid.graphics.Canvas;
importandroid.graphics.Matrix;
importandroid.util.AttributeSet;
importandroid.view.MotionEvent;
importandroid.view.VelocityTracker;
importandroid.view.View;
importandroid.view.ViewDebug.HierarchyTraceType;
importandroid.view.ViewGroup;
importandroid.widget.ImageView;
importandroid.widget.Scroller;
{
privateintmCurScreen=1;
//滑動的速度
privatestaticfinalintSNAP_VELOCITY=500;
;
privateintmWidth;
privateScrollermScroller;
privateCameramCamera;
privateMatrixmMatrix;
//旋轉的角度,可以進行修改來觀察效果
privatefloatangle=90;
publicRote3DView(Contextcontext,AttributeSetattrs){
super(context,attrs);
mScroller=newScroller(context);
mCamera=newCamera();
mMatrix=newMatrix();
initScreens();
}
publicvoidinitScreens(){
ViewGroup.LayoutParamsp=newViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
for(inti=0;i<3;i++){
this.addView(newImageView(this.getContext()),i,p);
}
((ImageView)this.getChildAt(0)).setImageResource(R.drawable.page1);
((ImageView)this.getChildAt(1)).setImageResource(R.drawable.page2);
((ImageView)this.getChildAt(2)).setImageResource(R.drawable.page3);
}
@Override
protectedvoidonLayout(booleanchanged,intl,intt,intr,intb){
intchildLeft=0;
finalintchildCount=getChildCount();
for(inti=0;i<childCount;i++){
finalViewchildView=getChildAt(i);
if(childView.getVisibility()!=View.GONE){
finalintchildWidth=childView.getMeasuredWidth();
childView.layout(childLeft,0,childLeft+childWidth,childView.getMeasuredHeight());
childLeft+=childWidth;
}
}
}
@Override
protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
finalintwidth=MeasureSpec.getSize(widthMeasureSpec);
finalintwidthMode=MeasureSpec.getMode(widthMeasureSpec);
if(widthMode!=MeasureSpec.EXACTLY){
thrownewIllegalStateException("僅支持精確尺寸");
}
finalintheightMode=MeasureSpec.getMode(heightMeasureSpec);
if(heightMode!=MeasureSpec.EXACTLY){
thrownewIllegalStateException("僅支持精確尺寸");
}
finalintcount=getChildCount();
for(inti=0;i<count;i++){
getChildAt(i).measure(widthMeasureSpec,heightMeasureSpec);
}
scrollTo(mCurScreen*width,0);
}
privatefloatmDownX;
@Override
publicbooleanonTouchEvent(MotionEventevent){
if(mVelocityTracker==null){
mVelocityTracker=VelocityTracker.obtain();
}
//將當前的觸摸事件傳遞給VelocityTracker對象
mVelocityTracker.addMovement(event);
floatx=event.getX();
switch(event.getAction()){
caseMotionEvent.ACTION_DOWN:
if(!mScroller.isFinished()){
mScroller.abortAnimation();
}
mDownX=x;
break;
caseMotionEvent.ACTION_MOVE:
intdisX=(int)(mDownX-x);
mDownX=x;
scrollBy(disX,0);
break;
caseMotionEvent.ACTION_UP:
=mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000);
intvelocityX=(int)velocityTracker.getXVelocity();
if(velocityX>SNAP_VELOCITY&&mCurScreen>0){
snapToScreen(mCurScreen-1);
}elseif(velocityX<-SNAP_VELOCITY&&mCurScreen<getChildCount()-1){
snapToScreen(mCurScreen+1);
}else{
snapToDestination();
}
if(mVelocityTracker!=null){
mVelocityTracker.recycle();
mVelocityTracker=null;
}
break;
}
returntrue;
}
@Override
publicvoidcomputeScroll(){
if(mScroller.computeScrollOffset()){
scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
postInvalidate();
}
}
publicvoidsnapToDestination(){
setMWidth();
finalintdestScreen=(getScrollX()+mWidth/2)/mWidth;
snapToScreen(destScreen);
}
publicvoidsnapToScreen(intwhichScreen){
whichScreen=Math.max(0,Math.min(whichScreen,getChildCount()-1));
setMWidth();
intscrollX=getScrollX();
intstartWidth=whichScreen*mWidth;
if(scrollX!=startWidth){
intdelta=0;
intstartX=0;
if(whichScreen>mCurScreen){
setPre();
delta=startWidth-scrollX;
startX=mWidth-startWidth+scrollX;
}elseif(whichScreen<mCurScreen){
setNext();
delta=-scrollX;
startX=scrollX+mWidth;
}else{
startX=scrollX;
delta=startWidth-scrollX;
}
mScroller.startScroll(startX,0,delta,0,Math.abs(delta)*2);
invalidate();
}
}
privatevoidsetNext(){
intcount=this.getChildCount();
Viewview=getChildAt(count-1);
removeViewAt(count-1);
addView(view,0);
}
privatevoidsetPre(){
intcount=this.getChildCount();
Viewview=getChildAt(0);
removeViewAt(0);
addView(view,count-1);
}
privatevoidsetMWidth(){
if(mWidth==0){
mWidth=getWidth();
}
}
}
實現立體效果,添加如下代碼:
/*
*當進行View滑動時,會導致當前的View無效,該函數的作用是對View進行重新繪制調用drawScreen函數
*/
@Override
protectedvoiddispatchDraw(Canvascanvas){
finallongdrawingTime=getDrawingTime();
finalintcount=getChildCount();
for(inti=0;i<count;i++){
drawScreen(canvas,i,drawingTime);
}
}
publicvoiddrawScreen(Canvascanvas,intscreen,longdrawingTime){
//得到當前子View的寬度
finalintwidth=getWidth();
finalintscrollWidth=screen*width;
finalintscrollX=this.getScrollX();
//偏移量不足的時
if(scrollWidth>scrollX+width||scrollWidth+width<scrollX){
return;
}
finalViewchild=getChildAt(screen);
finalintfaceIndex=screen;
finalfloatcurrentDegree=getScrollX()*(angle/getMeasuredWidth());
finalfloatfaceDegree=currentDegree-faceIndex*angle;
if(faceDegree>90||faceDegree<-90){
return;
}
finalfloatcenterX=(scrollWidth<scrollX)?scrollWidth+width
:scrollWidth;
finalfloatcenterY=getHeight()/2;
finalCameracamera=mCamera;
finalMatrixmatrix=mMatrix;
canvas.save();
camera.save();
camera.rotateY(-faceDegree);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX,-centerY);
matrix.postTranslate(centerX,centerY);
canvas.concat(matrix);
drawChild(canvas,child,drawingTime);
canvas.restore();
}
B. android屏幕旋轉,webview重新載入
在create時候加個狀態判斷
protected void onCreate(Bundle savedInstanceState){
...
if (savedInstanceState == null)
{
mWebView.loadUrl("your_url");
}
...
}
2. 重載保存狀態的函數:
@Override
protected void onSaveInstanceState(Bundle outState )
{
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
mWebView.restoreState(savedInstanceState);
}
C. Android 感測器的 數據流和框架 是怎麼樣讓 屏幕旋轉
這篇文章寫的感測器數據從驅動傳遞到應用程序的整個流程,還有數據校正的問題。
應用程序怎麼樣設置可以讓自己隨著設備的傾斜度變化而旋轉方向呢?在AndroidManifest.xml文件中的android:screenOrientation就可以了。這里追蹤一下它的內部機制。
先看一個最關鍵的部件:/frameworks/base/core/java/android/view/WindowOrientationListener.java
這個介面注冊一個accelerator,並負責把accelerator的數據轉化為orientation。這個API對應用程序不公開,我看Android2.3的源碼時發現只有PhoneWindowManager使用到它了。
/frameworks/base/policy/../PhoneWindowManager.java
PhonwWindowManager注冊了一個WindowOrientationListener,就可以非同步獲取當前設備的orientation了。再結合應用程序在AndroidManifest.xml中設置的值來管理著應用程序界面的旋轉方向。以下是PhoneWindowManager.java中相關的兩個代碼片段。
[java] view plain
public void onOrientationChanged(int rotation) {
// Send updates based on orientation value
if (localLOGV) Log.v(TAG, "onOrientationChanged, rotation changed to " +rotation);
try {
mWindowManager.setRotation(rotation, false,
mFancyRotationAnimation);
} catch (RemoteException e) {
// Ignore
}
}
... ...
switch (orientation) {//這個值就是當前設備屏幕的旋轉方向,再結合應用程序設置的android:configChanges屬性值就可以確定應用程序界面的旋轉方向了。應用程序設置值的優先順序大於感測器確定的優先順序。
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
//always return portrait if orientation set to portrait
return mPortraitRotation;
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
//always return landscape if orientation set to landscape
return mLandscapeRotation;
case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
//always return portrait if orientation set to portrait
return mUpsideDownRotation;
case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
//always return seascape if orientation set to reverse landscape
return mSeascapeRotation;
case ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE:
//return either landscape rotation based on the sensor
mOrientationListener.setAllow180Rotation(
isLandscapeOrSeascape(Surface.ROTATION_180));
return getCurrentLandscapeRotation(lastRotation);
case ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT:
mOrientationListener.setAllow180Rotation(
!isLandscapeOrSeascape(Surface.ROTATION_180));
return getCurrentPortraitRotation(lastRotation);
}
讓應用程序隨屏幕方向自動旋轉的實現原理就這么交待完了。我解決這一步時也沒有費多少力氣,在板子上打開SensorTest,對比一下XYZ三個軸和MileStone上面的數據,修改一下正負值就可以了。但要解決Teeter運行時Z軸反轉的問題,還得深層次挖一挖。
PhoneWindowManager.java中有這么一句:
mWindowManager.setRotation(rotation, false, mFancyRotationAnimation);
當PhonewindowManager通過WindowOrientationListener這個監聽器得知屏幕方向改變時,會通知給WindowManagerService(/frameworks/base/service/../WindowManagerService.java)
WindowManagerService中有這么一個監聽器集合:mRotationWatchers,誰想監聽屏幕方向改變,就會在這里注冊一個監聽器。SensorManager就這么幹了。然後,通過下面這個非同步方法獲知當前的屏幕方向
D. 如何使android布局webview gif旋轉90度
可以使用android 的animation類,可以進行旋轉和一些基本的向左向右的動作。
E. Android 旋轉動畫
<rotate
android:fromDegrees="45"//起始旋轉的角度
android:toDegrees="89"//結束選裝後的角度
android:ration="500"//執行時間為500ms
android:pivotX="50%"//距離控制項左邊緣50%
android:pivotY="50%"//距離控制項上邊緣50%(與上邊結合就是控制項中心)
android:fillEnabled="true"
android:fillAfter="true"//動畫執行完後停留在執行完的狀態
/>
—————————————————————————————————————————
當然也可以通過代碼用animation實現
好久沒寫,應該是
RotateAnimationanimation=newRotateAnimation(0f,45f,Animation.RELATIVE_TO_SELF,
0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(500);
view.setAnimation(animation);
F. android怎麼實現圖片旋轉
可以使用RotateAnimation動畫實現,設定無限循環即可
代碼如下
{
ImageViewiv;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
iv=(ImageView)findViewById(R.id.image);
RotateAnimationanimation=newRotateAnimation(0,360);
animation.setDuration(100000);//設定轉一圈的時間
animation.setRepeatCount(Animation.INFINITE);//設定無限循環
animation.setRepeatMode(Animation.RESTART);
iv.startAnimation(animation);
}
}
也可以自定義view繼承於imageview,啟動一個線程,在while循環里設置view的旋轉角度
{
privatefloatmCurDegree=0;//當前旋轉角度
publicRotateView(Contextcontext,AttributeSetattrs){
super(context,attrs);
newThread(this).start();
}
@Override
protectedvoidonLayout(booleanchanged,intleft,inttop,intright,
intbottom){
super.onLayout(changed,left,top,right,bottom);
//設定旋轉中心
setPivotX(getMeasuredWidth()/2);
setPivotY(getMeasuredHeight()/2);
}
@Override
publicvoidrun(){
while(true){
setRotation(mCurDegree);
mCurDegree+=5;
postInvalidate();
SystemClock.sleep(16);
}
}
}
在布局文件里使用RotateView代替imageview即可
G. android中怎麼定義旋轉動畫的旋轉速度
android源代碼之Rotate旋轉動畫
標簽為旋轉節點
Tween一共為我們提供了3種動畫渲染模式。
android:interpolator="@android:anim/accelerate_interpolator" 設置動畫渲染器為加速動畫(動畫播放中越來越快)
android:interpolator="@android:anim/decelerate_interpolator" 設置動畫渲染器為減速動畫(動畫播放中越來越慢)
android:interpolator="@android:anim/accelerate_decelerate_interpolator" 設置動畫渲染器為先加速在減速(開始速度最快 逐漸減慢)
如果不寫的話 默認為勻速運動
android:fromDegrees="+360"設置動畫開始的角度
android:toDegrees="0"設置動畫結束的角度
這個動畫布局設置動畫將向左做360度旋轉加速運動。
android:interpolator="@android:anim/accelerate_interpolator"
android:fromDegrees="+360"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:ration="2000"
/>
復制代碼
代碼實現
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class RotateActivity extends Activity {
/**向左旋轉動畫按鈕**/
Button mButton0 = null;
/**向右旋轉動畫按鈕**/
Button mButton1 = null;
/**顯示動畫的ImageView**/
ImageView mImageView = null;
/**向左旋轉動畫**/
Animation mLeftAnimation = null;
/**向右旋轉動畫**/
Animation mRightAnimation = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.retate);
/**拿到ImageView對象**/
mImageView = (ImageView)findViewById(R.id.imageView);
/**載入向左與向右旋轉動畫**/
mLeftAnimation = AnimationUtils.loadAnimation(this, R.anim.retateleft);
mRightAnimation = AnimationUtils.loadAnimation(this, R.anim.retateright);
mButton0 = (Button)findViewById(R.id.button0);
mButton0.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
/**播放向左旋轉動畫**/
mImageView.startAnimation(mLeftAnimation);
}
});
mButton1 = (Button)findViewById(R.id.button1);
mButton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
/**播放向右旋轉動畫**/
mImageView.startAnimation(mRightAnimation);
}
});
}
}
H. android開發中如何旋轉布局
樓主你好,這個可以通過動畫來達到這個效果的,代碼如下:
只要把您的layout對象傳進去就行了
public void showAnimation(View mView)
{
final float centerX = mView.getWidth() / 2.0f;
final float centerY = mView.getHeight() / 2.0f;
//這個是設置需要旋轉的角度,我設置的是180度
RotateAnimation rotateAnimation = new RotateAnimation(0, 180, centerX,
centerY);
//這個是設置通話時間的
rotateAnimation.setDuration(1000*3);
rotateAnimation.setFillAfter(true);
mView.startAnimation(rotateAnimation);
}