2016-07-23 53 views
11

我想,使其適合恰當下面另一個圖像旋轉圖像旋轉的ImageView的。所以,這就是我想實現:順時針使用ObjectAnimator

What I am trying to achieve

而且這是我能夠做到:

Currently achieved

它主要包含兩個ImageView與包含部分的第一個地板上方和第二個包含瓷磚覆蓋地板。

我的問題是,我無法進行動畫在順時針方向上的瓦片圖像以及繞x軸的視圖,使得它適合含在地板上方的部分中的圖像的下方。

我使用ObjectAnimator動畫我ImageView。下面是我的代碼:

public class VisualizerFragment extends Fragment { 

ImageView image; 
FlipImageView image_back; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    getActivity().setRequestedOrientation(
      ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

    View view = inflater.inflate(R.layout.fragment_visualizer, container, false); 
    image = (ImageView) view.findViewById(R.id.vs_image); 
    image_back = (FlipImageView) view.findViewById(R.id.vs_image_back); 

    Glide.with(getActivity()) 
      .load(R.drawable.floor3) 
      .centerCrop() 
      .fitCenter() 
      .listener(new RequestListener<Integer, GlideDrawable>() { 
       @Override 
       public boolean onException(Exception e, Integer model, Target<GlideDrawable> target, boolean isFirstResource) { 
        return false; 
       } 

       @Override 
       public boolean onResourceReady(GlideDrawable resource, Integer model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) { 
        Bitmap bmp = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.i9004); 

        //crop image 
        Matrix matrix = new Matrix(); 
        matrix.postScale(1f, 1f); 
        Bitmap croppedBitmap = Bitmap.createBitmap(bmp, 0, 0,100, 100, matrix, true); 

        bmp = getRoundedCornerBitmap(croppedBitmap, getResources().getColor(R.color.line),0,1,getActivity()); 
        BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp); 
        bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); 

        image_back.setDrawable(bitmapDrawable); 
        image_back.setFlippedDrawable(bitmapDrawable); 
        image_back.setFlipped(true); 
        image_back.toggleFlip(); 

        //rotate image 
        ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.loadAnimator(getActivity(), R.animator.fliping); 
        anim.setTarget(image_back); 
        anim.setDuration(100); 
        anim.start(); 


        return false; 

       } 
      }) 
      .into(image); 


    return view; 
} 


public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int color, int cornerDips, int borderDips, Context context) { 
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), 
      Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(output); 

    final int borderSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) borderDips, 
      context.getResources().getDisplayMetrics()); 
    final int cornerSizePx = 0; 
    final Paint paint = new Paint(); 
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
    final RectF rectF = new RectF(rect); 

    // prepare canvas for transfer 

    paint.setAntiAlias(true); 
    paint.setColor(0xFFFFFFFF); 
    paint.setStyle(Paint.Style.FILL); 
    canvas.drawARGB(0, 0, 0, 0); 
    canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint); 

    // draw bitmap 
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
    canvas.drawBitmap(bitmap, rect, rect, paint); 


    // draw border 
    paint.setColor(color); 
    paint.setStyle(Paint.Style.STROKE); 
    paint.setStrokeWidth((float) borderSizePx); 
    canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint); 

    return output; 
} 
} 

及以下用於動畫我fliping.xml:

<?xml version="1.0" encoding="utf-8"?> 
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" 
    android:propertyName="rotationX" 
    android:valueFrom="20" 
    android:valueTo="50" 
    android:valueType="floatType" 
/> 
+0

你得到一個錯誤?它是否在旋轉,或什麼都沒有發生? –

+0

請我沒有得到你,對不起,你能詳細的圖像越多 – Elltz

+0

看到差異....第一個希望,第二個是輸出......他需要輸出作爲第一圖像 –

回答

0

試試下面的代碼後,只需加載圖像

 ObjectAnimator imageViewObjectAnimator = ObjectAnimator.ofFloat(imageview , 
       "rotation", 20f, 50f); 
     imageViewObjectAnimator.setDuration(1000); // miliseconds 
     imageViewObjectAnimator.start(); 
+0

沒有圖像沒有按要求輸出 –

3

以及我發現一個解決方案....除去對象動畫,而使用這個代碼

camera.save(); 

      camera.rotateX(790); 
      camera.rotateY(2.5f); 
      camera.rotateZ(140); 

      camera.translate(170,0, -8); 

      camera.getMatrix(matrix); 
      camera.restore(); 

創建自定義圖像類和applyTransformation使用()