【问题标题】:How to set wallpaper to whole screen of device in android如何在android中将壁纸设置为设备的整个屏幕
【发布时间】:2014-03-20 10:56:36
【问题描述】:

我在我的 Android 应用程序中使用设置作为壁纸。但是当我将图像设置为墙纸时,它会在设备上放大到一定程度。当我将图像设置为墙纸时,我想要。这适合每个屏幕设备。我正在使用 DisplayMetrices,但它并不完美。

代码-

   public class FullImageActivity extends Activity {

    int position, width, height;
    LinearLayout full;
    Button btn;
    Context context;
    DisplayMetrics metrics;

    public Integer[] mThumbId = {
                R.drawable.kri1, R.drawable.kri2,
                R.drawable.kri3, R.drawable.kri4,
                R.drawable.kri5, R.drawable.kri6,
                R.drawable.kri7, R.drawable.kri8,
                R.drawable.kri9
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.full_image);

        // get intent data
        Intent i = getIntent();

        // Selected image id
        position = i.getExtras().getInt("id");        
        full = (LinearLayout) findViewById(R.id.full);
        btn = (Button)findViewById(R.id.btn);
        changeBackground();
        metrics = this.getResources().getDisplayMetrics();
        width = metrics.widthPixels;
        height = metrics.heightPixels;
        btn.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View arg0) {
            WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
            try {
                myWallpaperManager.suggestDesiredDimensions(width, height);
                myWallpaperManager.setResource(mThumbId[position]);
            } catch (IOException e) {
                e.printStackTrace();
           }
        }});
        ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this);
        full.setOnTouchListener(activitySwipeDetector);
    }

    private void changeBackground(){
        full.setBackgroundResource(mThumbId[position]);
   }

    public class ActivitySwipeDetector implements View.OnTouchListener {

        static final String logTag = "ActivitySwipeDetector";
        static final int MIN_DISTANCE = 100;
        private float downX, upX;
        Activity activity;

        public ActivitySwipeDetector(Activity activity){
            this.activity = activity;
        }

        public void onRightToLeftSwipe(){
            Log.i(logTag, "RightToLeftSwipe!");
            if(position < mThumbId.length - 1){
                position++;
                 changeBackground();
            }
    }

        public void onLeftToRightSwipe(){
            Log.i(logTag, "LeftToRightSwipe!");
            if(position > 0){
                position--;
                changeBackground();
           }
        }

        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()){
                case MotionEvent.ACTION_DOWN: {
                    downX = event.getX();
                    return true;
                }
                case MotionEvent.ACTION_UP: {
                    upX = event.getX();

                    float deltaX = downX - upX;

                    // swipe horizontal?
                    if(Math.abs(deltaX) > MIN_DISTANCE){
                        // left or right
                        if(deltaX < 0) { this.onLeftToRightSwipe(); return true; }
                        if(deltaX > 0) { this.onRightToLeftSwipe(); return true; }
                    }
                    else {
                            Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
                            return false; // We don't consume the event
                    }


                    return true;
                }
            }
            return false;
        }
    }
}

提前致谢。

【问题讨论】:

  • 使用不同尺寸的图像,以便它们可以支持所有屏幕。
  • 我不想使用不同尺寸的图片。因为我不希望我的应用太大。
  • 哦...所以您可以使用大尺寸图像并使用显示指标为不同分辨率使用分辨率代码...
  • 我已经尝试过了,但它不起作用。
  • @krishna 查看已编辑的帖子。

标签: android image fullscreen wallpaper


【解决方案1】:

我在我的应用程序中使用这个库
它有效且简单

CropImage

它是开源的,因此您可以根据需要编辑库
玩得开心

【讨论】:

  • 我不想裁剪图像。
【解决方案2】:

试试这个-

Bitmap bmap = BitmapFactory.decodeStream(getResources().openRawResource(mThumb[position]));
DisplayMetrics metrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels; 
int width = metrics.widthPixels;
Bitmap yourbitmap = Bitmap.createScaledBitmap(bmap, width, height, true); 
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); 
try {
  wallpaperManager.setBitmap(yourbitmap);
  } catch (IOException e) {
  e.printStackTrace();
}

【讨论】:

    【解决方案3】:

    我猜你需要一个保持图像纵横比的Image 缩放算法, 将您拥有的最佳图像存储在您的可绘制文件夹中,并让此Algorithm 缩小最佳图像以适合设备的HeightWidth,保持原始图像的纵横比。

     Bitmap scaleDownLargeImageWithAspectRatio(Bitmap image)
                {
                    int imaheVerticalAspectRatio,imageHorizontalAspectRatio;
                    float bestFitScalingFactor=0;
                    float percesionValue=(float) 0.2;
    
                    //getAspect Ratio of Image
                    int imageHeight=(int) (Math.ceil((double) image.getHeight()/100)*100);
                    int imageWidth=(int) (Math.ceil((double) image.getWidth()/100)*100);
                    int GCD=BigInteger.valueOf(imageHeight).gcd(BigInteger.valueOf(imageWidth)).intValue();
                    imaheVerticalAspectRatio=imageHeight/GCD;
                    imageHorizontalAspectRatio=imageWidth/GCD;
                    Log.i("scaleDownLargeImageWIthAspectRatio","Image Dimensions(W:H): "+imageWidth+":"+imageHeight);
                    Log.i("scaleDownLargeImageWIthAspectRatio","Image AspectRatio(W:H): "+imageHorizontalAspectRatio+":"+imaheVerticalAspectRatio);
    
                    //getContainer Dimensions
                    int displayWidth = getWindowManager().getDefaultDisplay().getWidth();
                    int displayHeight = getWindowManager().getDefaultDisplay().getHeight();
                   //I wanted to show the image to fit the entire device, as a best case. So my ccontainer dimensions were displayWidth & displayHeight. For your case, you will need to fetch container dimensions at run time or you can pass static values to these two parameters 
    
                    int leftMargin = 0;
                    int rightMargin = 0;
                    int topMargin = 0;
                    int bottomMargin = 0;
                    int containerWidth = displayWidth - (leftMargin + rightMargin);
                    int containerHeight = displayHeight - (topMargin + bottomMargin);
                    Log.i("scaleDownLargeImageWIthAspectRatio","Container dimensions(W:H): "+containerWidth+":"+containerHeight);
    
                    //iterate to get bestFitScaleFactor per constraints
                    while((imageHorizontalAspectRatio*bestFitScalingFactor <= containerWidth) && 
                            (imaheVerticalAspectRatio*bestFitScalingFactor<= containerHeight))
                    {
                        bestFitScalingFactor+=percesionValue;
                    }
    
                    //return bestFit bitmap
                    int bestFitHeight=(int) (imaheVerticalAspectRatio*bestFitScalingFactor);
                    int bestFitWidth=(int) (imageHorizontalAspectRatio*bestFitScalingFactor);
                    Log.i("scaleDownLargeImageWIthAspectRatio","bestFitScalingFactor: "+bestFitScalingFactor);
                    Log.i("scaleDownLargeImageWIthAspectRatio","bestFitOutPutDimesions(W:H): "+bestFitWidth+":"+bestFitHeight);
                    image=Bitmap.createScaledBitmap(image, bestFitWidth,bestFitHeight, true);
    
                    //Position the bitmap centre of the container
                    int leftPadding=(containerWidth-image.getWidth())/2;
                    int topPadding=(containerHeight-image.getHeight())/2;
                    Bitmap backDrop=Bitmap.createBitmap(containerWidth, containerHeight, Bitmap.Config.RGB_565);
                    Canvas can = new Canvas(backDrop);
                    can.drawBitmap(image, leftPadding, topPadding, null);
    
                    return backDrop;
                }
    
    
    private void changeBackground()
        {
            Drawable inputDrawable = mThumbId[position];
            Bitmap bitmap = ((BitmapDrawable)inputDrawable).getBitmap();
            bitmap = scaleDownLargeImageWithAspectRatio(bitmap);
            @SuppressWarnings("deprecation")
            Drawable outDrawable=new BitmapDrawable(bitmap);  
            full.setBackground(outDrawable);
        }
    

    【讨论】:

    • 位图位图 = ((BitmapDrawable)mThumbId[position]).getBitmap();在这条线上。无法将位图转换为整数错误。
    • 我在答案的底部添加了 changeBackground()。请检查并提供反馈。
    • 我在按钮点击设置壁纸你不认为我必须在按钮点击中包含任何代码。
    • 小伙伴,在按钮的onClick方法中调用changeBackground()函数。
    【解决方案4】:

    尝试使用此代码将图像设置为墙纸

        public void setWallpaper(final Bitmap bitmp)
        {
            int  screenWidth=getWallpaperDesiredMinimumWidth();
            int    screenHeight=getWallpaperDesiredMinimumHeight();
                    try {
    
                    WallpaperManager    wallpaperManager = WallpaperManager.getInstance(this);
    
    
                            Bitmap btm = getResizedBitmap(bitmp, screenHeight, screenWidth);
                            wallpaperManager.setBitmap(btm);
                        Toast toast=Toast.makeText(this, "Done", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.TOP|Gravity.CENTER, 0, 0);
                        toast.show();
    
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
        }
    

     public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    
                int width = bm.getWidth();
    
                int height = bm.getHeight();
    
                float scaleWidth = ((float) newWidth) / width;
    
                float scaleHeight = ((float) newHeight) / height;
    
                /**
                 *  create a matrix for the manipulation
                 */
    
                Matrix matrix = new Matrix();
    
                /**
                 *  resize the bit map
                 */
    
                matrix.postScale(scaleWidth, scaleHeight);
    
                /**
                 * recreate the new Bitmap
                 */
    
                Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    
                return resizedBitmap;
    
                }
    

    编辑:

    您只需要使用所需的位图调用该函数

    btn.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View arg0) {
             setWallpaper(BitmapFactory.decodeResource(FullImageActivity.this.getResources(),
                                           mThumbId[position]));
        }});
    

    【讨论】:

    • @JohnR:如果你真的不知道在哪里使用上面的代码,那么我想你真的需要在开始之前学习 android 的基础知识。
    • @MehulJoisar 我发现很难在我的代码中编写此代码。当我在 onClick 方法中使用 set setWallpaper(mThumbId[position]) 调用时,它显示错误将数组类型转换为位图。
    • @MehulJoisar 静止图像尺寸大于屏幕尺寸。
    【解决方案5】:
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    

    【讨论】:

      猜你喜欢
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多