2016-02-12 89 views
2

我正在設計(試圖學習)Android的簡單小部件。 我:旋轉ImageView

public class NewAppWidget extends AppWidgetProvider { 

這個類裏面,我有簡單的代碼:

public double angle(Calendar calendarDate) { 
     //Earth rotation; 
     int day = calendarDate.get(Calendar.DAY_OF_YEAR); 
     int angle; 
     angle = (day/365) * 360; 
     return angle; 
    } 

然後,我要選擇一個圖像,並通過ImageView的那個角度,這完全不起作用旋轉。 ..

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, 
          int appWidgetId) { 



// Construct the RemoteViews object 

ImageView image = (ImageView) findViewById(R.layout.new_app_widget); 

    image.setImageResource(R.drawable.moon10); 
    image.setRotation(angle); 

    // Instruct the widget manager to update the widget 
    appWidgetManager.updateAppWidget(appWidgetId, new_app_widget); 

} 

那裏可能有幾個錯誤 - 我是一個初學者。

由Android工作室強調的三個錯誤是:

  1. findViewById紅色(不能解析法)紅色
  2. 角(不能解析符號的角度)
  3. new_app_widget在最後一行紅色(不能解決符號)

感謝您的幫助和美好的一天。

JY

+0

注意:對於您的標題1)Android Studio只是一個IDE,它與問題無關。 2)Android代碼(主要是)Java,所以你不需要說「Java代碼」 –

+0

我投票重新打開這個問題,因爲它不是重複的:它不是關於旋轉「正常」視圖,因爲OP想要旋轉一個ImageView,它是RemoteViews的一部分,其中(請參閱我的答案)「View.setRotation()」是不可能的。 OP接受「重複」主要是出於缺乏經驗,但是尋找涉及應用小部件(主屏幕小部件)的解決方案的未來讀者不應該被引導到鏈接的帖子,而是被引導到這個。 – 0X0nosugar

回答

0
  1. 的方法是靜態的,你需要創建視圖findviewbyid(); 或使旋轉無效的非靜態
  2. 角度必須是int,不是雙
0

爲獲得方向,使用下面的功能。只是通過圖像路徑的情況下和URI

public static int getGalleryOrientation(Context context, Uri path) { 
    /* it's on the external media. */ 
    Cursor cursor = context.getContentResolver().query(path, 
      new String[]{MediaStore.Images.ImageColumns.ORIENTATION}, 
      null, null, null); 

    if (cursor.getCount() != 1) { 
     return -1; 
    } 

    cursor.moveToFirst(); 
    return cursor.getInt(0); 

} 
+0

我不明白這是如何解決這個問題的......以及如何從一個'R.drawable'獲得一個URI? –

+0

謝謝@ cricket_007教導如何發送答案。 – jigspatel

+1

我所做的只是重新格式化你的答案,我仍然不明白 –

0

TL; DR

這是不可能的使用方法View.setRotation(float)任何View這是一個主屏幕小部件的一部分,至少這是什麼我的IDE告訴我:

W/AppWidgetHostView:updateAppWidget找不到任何觀點,用錯誤觀點 android.widget.RemoteV IEWS $ ActionException:觀點:android.widget.ImageView不能使用方法與RemoteViews:setRotation(浮動)

話雖這麼說,讓我們至少可以得到您的應用程序插件運行起來:)

你需要一個帶有ImageView的佈局文件,我們稱之爲new_app_widget。XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:padding="@dimen/widget_margin" 
      android:background="#09C"> 
    <ImageView 
     android:layout_width="24dp" 
     android:layout_height="24dp" 
     android:background="#ffffff" 
     android:id="@+id/imageView" 
     android:layout_gravity="center" 
     /> 

</FrameLayout> 

更改您這樣的代碼:

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, 
         int appWidgetId) { 

    // Construct the RemoteViews object 
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget); 

    // use the RemoteViews methods to access the children of the FrameLayout: 
    views.setImageViewResource(R.id.imageView, R.drawable.moon10); 


    // Instruct the widget manager to update the widget 
    appWidgetManager.updateAppWidget(appWidgetId, views); 

} 

如果你需要一些輪換,你將不得不與旋轉Drawable呼叫setImageViewResource()像上面的Drawable執行它,然後。 也Rotating a drawable in Android

見最後,大約爲紅色

角(不能解析符號的角度)

您已經定義了一個方法angle(Calendar)。如果你想使用它,你需要用一些Calendar輸入來調用它。

+1

啊,我認爲ImageView只是一個普通的視圖,我的不好! –

+0

雖然我讀了,但我們應該避免像「謝謝」這樣的評論,我忍不住要說出口!它工作完全,問題解決了,偉大的一週結束。 –

+0

@ Jean-Yves - 感謝您告訴我:)通過點擊答案左上角的複選標記,您可以指出答案解決了您的問題。當然,你最終也可以贊成它。這一方面會爲我帶來一些「聲譽點」。另一方面,對於未來尋找解決方案來解決他們自己的問題的人來說,這對(問題和答案)將更容易找到。如果「重複」狀態阻止任何這些行爲,那麼沒關係。這很有趣:D – 0X0nosugar