2016-02-12 96 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