相关疑难解决方法(0)

如何获得android.widget.ImageView的宽度和高度?

????????????????????????????????????????????????   ^
? ImageView    ????????????????                ?   |
?              ?              ?                ?   |
?              ? Actual image ?                ?   |
?              ?              ?                ?   |60px height of ImageView
?              ?              ?                ?   |
?              ?              ?                ?   |
?              ????????????????                ?   |
????????????????????????????????????????????????   
<------------------------------------------------>
                   90px width of ImageView
Run Code Online (Sandbox Code Playgroud)

我有一个默认高度和宽度的图像视图,图像存储在数据库中,我想根据Imageview高度宽度缩放图像.因为我不希望它给出默认值,因为当我改变它的高度和宽度时我也必须在代码中更改它.

我试图获得ImageView的高度和宽度,但在两种情况下都返回0.

int height = ((ImageView) v.findViewById(R.id.img_ItemView)).getHeight();
Run Code Online (Sandbox Code Playgroud)

即使它有默认的高度和宽度,这也会返回0

height android image width

270
推荐指数
7
解决办法
14万
查看次数

如何在Android中进行流畅的图像旋转?

我正在使用a RotateAnimation来旋转我在Android中用作自定义循环微调器的图像.这是我的rotate_indefinitely.xml文件,我放入res/anim/:

<?xml version="1.0" encoding="UTF-8"?>
<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:duration="1200" />    
Run Code Online (Sandbox Code Playgroud)

当我将它应用于我的ImageView使用时AndroidUtils.loadAnimation(),效果很好!

spinner.startAnimation( 
    AnimationUtils.loadAnimation(activity, R.anim.rotate_indefinitely) );
Run Code Online (Sandbox Code Playgroud)

一个问题是图像旋转似乎在每个周期的顶部暂停.

换句话说,图像旋转360度,暂停,然后再旋转360度等.

我怀疑问题是动画是使用像android:iterpolator="@android:anim/accelerate_interpolator"(AccelerateInterpolator)的默认插值器,但我不知道如何告诉它不插入动画.

如何关闭插值(如果这确实是问题)以使我的动画循环顺利进行?

animation android view

209
推荐指数
10
解决办法
22万
查看次数

如何在使用大图像时使用JNI位图操作来帮助避免OOM?

背景

大多数时候,在Android上获取OOM是由于使用了太多位图和/或创建大位图.

最近我决定试用JNI,以便通过将数据本身存储在JNI端来避免OOM.

在与JNI搞砸了一段时间后,我在SO上创建了一些帖子,寻求帮助并分享我的知识,现在我决定与你分享更多代码.如果有人有兴趣阅读调查结果或做出贡献,这里有帖子:

这一次,我添加了存储,恢复,裁剪和旋转位图的功能.它应该很容易添加更多选项,如果其他人在这里将自己的代码添加到更有用的功能,我会很高兴.

所以我要展示的代码实际上是合并了我创造的所有东西.

示例使用代码:

Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
final int width=bitmap.getWidth(),height=bitmap.getHeight();
// store the bitmap in the JNI "world"
final JniBitmapHolder bitmapHolder=new JniBitmapHolder(bitmap);
// no need for the bitmap on the java "world", since the operations are done on the JNI "world"
bitmap.recycle();
// crop a center square from the bitmap, from (0.25,0.25) to (0.75,0.75) of the bitmap.
bitmapHolder.cropBitmap(width/4,height/4,width*3/4,height*3/4);
//rotate the bitmap:
bitmapHolder.rotateBitmapCcw90();
//get the output java bitmap , …
Run Code Online (Sandbox Code Playgroud)

java-native-interface android bitmap out-of-memory android-ndk

46
推荐指数
1
解决办法
3万
查看次数

旋转带动画的图像

图像的两种状态

是)我有的

我有一个箭头图像(如左图).当用户点击它时,它应该用动画旋转180度,看起来应该是正确的.

我做了什么

private void rotate(float degree, final int toggleV) {

        final RotateAnimation rotateAnim = new RotateAnimation(0.0f, degree,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);

        rotateAnim.setDuration(500);
        toggle.startAnimation(rotateAnim);
        rotateAnim.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {


                if (toggleV == 1)
                    toggle.setImageResource(R.drawable.toggle_up);
                else
                    toggle.setImageResource(R.drawable.toggle_down);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }
Run Code Online (Sandbox Code Playgroud)

问题

我看到动画工作正常,但设置图像时有一点闪烁.可能是因为动画结束和设置图像时的时差.

如何删除此闪烁问题?你有更好的方法吗?

android image-rotation android-animation android-imageview

23
推荐指数
1
解决办法
2万
查看次数

使用动态位图设置在Android上实现KenBurns效果

背景

我正在动作栏上实现" KenBurns效果 "(演示在这里),如图书馆的样本所示(移动的图标除外,我自己就是这样做的).

事实上,我甚至在很久以前(这里)问过这个问题,在这一点上我甚至都不知道它的名字.我确信我找到了解决方案,但它有一些问题.

此外,因为我有时从设备显示图像,有的甚至需要旋转,所以我用一个rotatableDrawable(如图所示这里).

问题

当前实现无法处理动态给出的多个位图(例如,来自Internet),甚至不能查看输入图像的大小.

相反,它只是以随机方式进行缩放和平移,因此很多时候它可以缩放太多/很少,并且可以显示空白空间.

代码

这是与问题相关的代码:

private float pickScale() {
    return MIN_SCALE_FACTOR + this.random.nextFloat() * (MAX_SCALE_FACTOR - MIN_SCALE_FACTOR);
}

private float pickTranslation(final int value, final float ratio) {
    return value * (ratio - 1.0f) * (this.random.nextFloat() - 0.5f);
}

public void animate(final ImageView view) {
    final float fromScale = pickScale();
    final float toScale = pickScale();
    final float fromTranslationX = pickTranslation(view.getWidth(), fromScale);
    final float fromTranslationY = pickTranslation(view.getHeight(), fromScale); …
Run Code Online (Sandbox Code Playgroud)

animation android image-manipulation android-animation android-kenburnsview

16
推荐指数
1
解决办法
7084
查看次数

如何在每次按钮点击时在imageview中旋转图像?

这是java代码.我从图库中获取图像.我有一个Button和一个ImageView.它只旋转一次.当我再次点击按钮时,它不是旋转图像.

public class EditActivity extends ActionBarActivity
{
private Button rotate;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit);
    rotate=(Button)findViewById(R.id.btn_rotate1);
    imageView = (ImageView) findViewById(R.id.selectedImage);
    String path = getIntent().getExtras().getString("path");
    final Bitmap bitmap = BitmapFactory.decodeFile(path);
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 510, 500,
            false));
    rotate.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
          {                  
            imageView.setRotation(90);


        }
    });



}
Run Code Online (Sandbox Code Playgroud)

java android imageview

9
推荐指数
1
解决办法
2万
查看次数

旋转位图会导致outOfMemoryException

我以这种方式旋转位图,在每个按钮上单击图像旋转90度

Matrix matrix = new Matrix();
matrix.postRotate(90);
rotated = Bitmap.createBitmap(rotated, 0, 0,
        rotated.getWidth(), rotated.getHeight(), matrix, true);
iv.setImageBitmap(rotated);
Run Code Online (Sandbox Code Playgroud)

我用很多图像试过这个,但是现在一个引起了OutOfMemoryError.有办法防止这种情况吗?当然我可以调用recycle,但后来我丢失了位图并且必须从imageview再次获取它.我认为这不会有任何区别.

android bitmap out-of-memory

8
推荐指数
1
解决办法
6687
查看次数

Android:在ImageView中旋转图像90度但没有延迟

我正在开发一款游戏,用户需要点击ImageView中的图像来旋转它.在每个拍子图像上顺时针旋转90度.但是图像需要时间从旧位置旋转到新位置.这妨碍了游戏体验.我使用了以下内容:

protected void onCreate(Bundle savedInstanceState)
{  
... 
...  
    imgview = (ImageView)findViewById(R.id.imageView1);  
    imgview.setOnClickListener(new OnClickListener() {
         @Override 
         public void onClick(View arg0) {
              Matrix matrix = new Matrix();
              matrix.postRotate(90); 
              Bitmap myImg = getBitmapFromDrawable(imgview.getDrawable());
              Bitmap rotated = Bitmap.createBitmap(myImg,0,0,myImg.getWidth(),myImg.getHeight(),matrix,true);
              imgview.setImageBitmap(rotated);
         }
});
Run Code Online (Sandbox Code Playgroud)

我想知道有没有其他方法旋转图像 而不会产生任何旋转延迟.

android bitmap rotation matrix imageview

8
推荐指数
3
解决办法
4万
查看次数

Android速度变化时如何旋转针?

我已经写了一个应用程序,我有一个speedometer带有a needle,我试图center用速度变化旋转针.

我从互联网上计算下载速度的速度值,该角度也是测量的,

我的问题是如何将针头一端固定在车速表的中心?

如何通过改变角度值来旋转针?

我期待这样

在此输入图像描述

这里针不固定中心位置如何像第一个图像中心一样固定图像的针中心

在此输入图像描述

android gauge

7
推荐指数
2
解决办法
2万
查看次数

相机意图自动旋转到90度

在我下面的代码中,我正在尝试使用原生相机拍照并上传到服务器但是当我把它作为肖像并在画廊中查看它作为景观意味着,它旋转到90度.请帮忙: -

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(intent, REQUEST_CAMERA);

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {

            handleCameraPhoto();
}
private void handleCameraPhoto() {
    Intent mediaScanIntent = new Intent(
            "android.intent.action.MEDIA_SCANNER_SCAN_FILE");
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);

    mediaScanIntent.setData(contentUri);
    getActivity().sendBroadcast(mediaScanIntent);
}
Run Code Online (Sandbox Code Playgroud)

如何在保存到SD卡之前旋转图像?

android

7
推荐指数
1
解决办法
2万
查看次数

当自定义ImageView调用startAnimation(Animation)时,为什么getActivity()在JUnit测试期间会阻塞?

我写了一个Android应用程序,显示一个自定义ImageView旋转自定义的自定义startAnimation(Animation).该应用程序工作正常,但如果我创建一个类型ActivityInstrumentationTestCase2和测试调用的JUnit测试getActivity(),该调用getActivity()永远不会返回,直到应用程序进入后台(例如,按下设备的主页按钮).

经过很长时间和挫折之后,我发现getActivity()如果我startAnimation(Animation)在自定义ImageView类中注释掉调用,则会立即返回.但这会破坏我的习惯的目的ImageView,因为我确实需要动画它.

任何人都可以告诉我为什么getActivity()在我的JUnit测试期间阻止但只有在startAnimation使用时?提前感谢任何能够建议解决方法或告诉我我做错了什么的人.

注意:解决方案需要至少使用Android API级别10.

以下是运行它所需的所有源代码(将任何PNG图像放在res/drawable中并将其命名为the_image.png):

activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <com.example.rotatingimageviewapp.RotatingImageView 
        android:id="@+id/rotatingImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/the_image" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

MainActivity.java:

package com.example.rotatingimageviewapp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

    private RotatingImageView rotatingImageView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rotatingImageView = (RotatingImageView) findViewById(
                R.id.rotatingImageView); …
Run Code Online (Sandbox Code Playgroud)

junit android imageview android-imageview android-testing

5
推荐指数
1
解决办法
2479
查看次数

Android - 旋转 ImageView 但宽度/高度不变?

如何让 ImageView 在旋转时更新其宽度/高度?

如果我有下面的 ImageView 并进行调试,宽度为 827,高度为 543。旋转后,宽度/高度保持不变,导致我的动画出现问题。

图像在屏幕上正确旋转,但我希望更新 ImageView 尺寸(宽度 = 543,高度 = 827)。

<ImageView
    android:id="@+id/imageView2"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_centerInParent="false"
    android:src="@drawable/img"
    android:adjustViewBounds="true"
    android:padding="0dp"
    android:background="@android:color/transparent"
    android:visibility="visible"
    android:layout_centerVertical="false" />

    _img = FindViewById<ImageView>(Resource.Id.imageView2); //width = 827, height = 543
    _img.Rotation = -90;
// width is still 827 and height is still 543 
Run Code Online (Sandbox Code Playgroud)

c# java android xamarin

5
推荐指数
0
解决办法
2352
查看次数

从imageview中心点旋转图像

我想在android中旋转图像.我发现这个有用的帖子,它的效果很好,但似乎在android中的旋转从左下角开始.我需要从中心点旋转图像.可能吗?代码相同是有帮助的.谢谢.

android android-imageview

2
推荐指数
1
解决办法
9213
查看次数