zoukankan      html  css  js  c++  java
  • 代码获取控件的截图+制造动画

    废话不多说先粘贴代码  下边这段代码是在按钮的点击事件里面写的:

    // 开启图片的缓存功能
    mGuidePager.setDrawingCacheEnabled(true);
    // 设置图片的质量
    mGuidePager.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    // 获取Bitmap
    Bitmap drawingCache = mGuidePager.getDrawingCache();
    // 获取上边的一半赋值给上边的ImageView
    Bitmap topBitmap = getTopBitmap(drawingCache);
    mTopImg.setImageBitmap(topBitmap);
                    
    // 获取下边的一半赋值给下边的ImageView
    Bitmap bottomBitmap = getBottomBitmap(drawingCache);
    mBottomImg.setImageBitmap(bottomBitmap);
    // 开启动画
    startAnim();

    接下来是获取上下两部分的截图的代码 是本次事例的关键所在:

    private Bitmap getTopBitmap(Bitmap drawingCache) {
            int height = drawingCache.getHeight() / 2;
            int width = drawingCache.getWidth();
            
            Bitmap.Config config = drawingCache.getConfig();
            Bitmap topBitmap = Bitmap.createBitmap(width, height, config);
            Canvas canvas = new Canvas(topBitmap);
            Matrix matrix = new Matrix();
            Paint paint = new Paint();
            //将图赋值到画布上
            canvas.drawBitmap(drawingCache, matrix, paint);
            return topBitmap;
        }
    private Bitmap getBottomBitmap(Bitmap drawingCache) {
            int height = drawingCache.getHeight() / 2;
            int width = drawingCache.getWidth();
            Bitmap.Config config = drawingCache.getConfig();
            Bitmap bottomBitmap = Bitmap.createBitmap(width, height, config);
            
            Canvas canvas = new Canvas(bottomBitmap);
            Matrix matrix = new Matrix();
            //原图向上移动宽度的一半  让下边的一半可以画到画布上
            matrix.setTranslate(0, -height);
            Paint paint = new Paint();
            canvas.drawBitmap(drawingCache, matrix, paint);
            return bottomBitmap;
        }

    再接下来是动画的代码  小意思了:

    private void startAnim() {
            mGuidePager.setVisibility(View.INVISIBLE);
            mAllAnim.setVisibility(View.VISIBLE);
            //添加动画到集合中
            AnimationSet as1 = new AnimationSet(false);
            TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, -mTopImg.getHeight());
            AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
            as1.addAnimation(translateAnimation);
            as1.addAnimation(alphaAnimation);
            as1.setDuration(1000);
            mTopImg.startAnimation(as1);
            
            TranslateAnimation translateAnimation1 = new TranslateAnimation(0, 0, 0, mBottomImg.getHeight());
            AlphaAnimation alphaAnimation1 = new AlphaAnimation(1.0f, 0.0f);
            AnimationSet animationSet = new AnimationSet(false);
            animationSet.addAnimation(translateAnimation1);
            animationSet.addAnimation(alphaAnimation1);
            animationSet.setDuration(1000);
            mBottomImg.setAnimation(animationSet);
            
        }

    总结一下:

    在这里给大家讲一下实现原理: 首先我们应该找到要获取截图的控件  我的事例中找的是一个ViewPager  他们的父布局最好是FrameLayout或者RelativeLayout 然后创建一个LinearLauout布局 覆盖在要获取截图的控件上方  然后在这个布局中添加两个ImageView,接下来在代码中将获取的两部分的截图分别赋值给这两个ImageView 并且分别为他俩添加动画即可。具体实现参见代码。

  • 相关阅读:
    Best Time to Buy and Sell Stock III
    Valid Palindrome
    Longest Substring Without Repeating Characters
    Copy List with Random Pointer
    Add Two Numbers
    Recover Binary Search Tree
    Anagrams
    ZigZag Conversion
    Merge k Sorted Lists
    Distinct Subsequences
  • 原文地址:https://www.cnblogs.com/yegong0214/p/7803883.html
Copyright © 2011-2022 走看看