android将老项目改为按屏幕大小自适应,只需2步
程序员文章站
2022-05-24 21:18:10
...
先看效果图
目标:老项目是320*480,改为按屏幕大小自适应。
1.主要是图片和坐标,我都放在一个类里了,改下包名,直接加入项目。
package wyf.ytl;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.DisplayMetrics;
public class ZoomImg {
public final static int widthOld = 320; // 原设计屏幕宽度
public final static int heithtOld = 480;
public static float widthScale = 1; // 原转为新 比例系数
public static float heightScale = 1; //原设计屏幕高度
public static float widthScaleTouch = 1; // 新转为原 比例系数 反向,比如触控坐标,原值不动,只缩放xy.
public static float heightScaleTouch = 1;
/**根据原设计屏幕 缩放图片 使能自适应
*
* @param bm 原图片
* @param activity 所在界面 。如果当前就是在Activity直接用this
* @return 自适应后的图片
*/
public static Bitmap zoomImgAuto(Bitmap bm, Activity activity) {
getScreen(activity);// 根据屏幕大小计算出比例系数
if (null != bm) {
return zoomImg(bm, (int) (bm.getWidth() * widthScale),
(int) (bm.getHeight() * heightScale));
}
return null;
}
/**根据屏幕大小计算出比例系数
*
* @param activity 所在界面
*/
public static void getScreen(Activity activity)
{
DisplayMetrics outMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
int widthPixels = outMetrics.widthPixels;
int heightPixels = outMetrics.heightPixels;
// Log.i(TAG, "widthPixels = " + widthPixels + ",heightPixels = " +
// heightPixels);
widthScale = (float) widthPixels / (float) widthOld;
heightScale = (float) heightPixels / (float) heithtOld;
widthScaleTouch = (float) widthOld / (float) widthPixels;
heightScaleTouch = (float) heithtOld / (float) heightPixels;
}
// 缩放图片
public static Bitmap zoomImg(String img, int newWidth, int newHeight) {
// 图片源
Bitmap bm = BitmapFactory.decodeFile(img);
if (null != bm) {
return zoomImg(bm, newWidth, newHeight);
}
return null;
}
public static Bitmap zoomImg(Context context, String img, int newWidth,
int newHeight) {
// 图片源
try {
Bitmap bm = BitmapFactory.decodeStream(context.getAssets()
.open(img));
if (null != bm) {
return zoomImg(bm, newWidth, newHeight);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
// 缩放图片
public static Bitmap zoomImg(Bitmap bm, int newWidth, int newHeight) {
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 计算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
true);
return newbm;
}
}
2.将所有坐标和图片都这样处理就可以了。
为了能一键替换,继续封装
package wyf.ytl;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.DisplayMetrics;
public class ZoomImg extends Canvas {
public final static int widthOld = 320; // 原设计屏幕宽度
public final static int heithtOld = 480;
public static float widthScale = 1; // 原转为新 比例系数
public static float heightScale = 1; //原设计屏幕高度
public static float widthScaleTouch = 1; // 新转为原 比例系数 反向,比如触控坐标,原值不动,只缩放xy.
public static float heightScaleTouch = 1;
public static void drawText(Canvas canvas,Activity activity,String text, float x, float y, Paint paint)
{
canvas.drawText(text, x*widthScale, y*heightScale, paint);//绘制图片
}
public static void drawBitmap(Canvas canvas,Activity activity,Bitmap bitmap, float left, float top, Paint paint)
{
canvas.drawBitmap(zoomImgAuto(bitmap,activity), left*widthScale, top*heightScale, null);//绘制图片
}
/**根据原设计屏幕 缩放图片 使能自适应
*
* @param bm 原图片
* @param activity 所在界面 。如果当前就是在Activity直接用this
* @return 自适应后的图片
*/
public static Bitmap zoomImgAuto(Bitmap bm, Activity activity) {
getScreen(activity);// 根据屏幕大小计算出比例系数
if (null != bm) {
return zoomImg(bm, (int) (bm.getWidth() * widthScale),
(int) (bm.getHeight() * heightScale));
}
return null;
}
/**根据屏幕大小计算出比例系数
*
* @param activity 所在界面
*/
public static void getScreen(Activity activity)
{
DisplayMetrics outMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
int widthPixels = outMetrics.widthPixels;
int heightPixels = outMetrics.heightPixels;
// Log.i(TAG, "widthPixels = " + widthPixels + ",heightPixels = " +
// heightPixels);
widthScale = (float) widthPixels / (float) widthOld;
heightScale = (float) heightPixels / (float) heithtOld;
widthScaleTouch = (float) widthOld / (float) widthPixels;
heightScaleTouch = (float) heithtOld / (float) heightPixels;
}
// 缩放图片
public static Bitmap zoomImg(String img, int newWidth, int newHeight) {
// 图片源
Bitmap bm = BitmapFactory.decodeFile(img);
if (null != bm) {
return zoomImg(bm, newWidth, newHeight);
}
return null;
}
public static Bitmap zoomImg(Context context, String img, int newWidth,
int newHeight) {
// 图片源
try {
Bitmap bm = BitmapFactory.decodeStream(context.getAssets()
.open(img));
if (null != bm) {
return zoomImg(bm, newWidth, newHeight);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
// 缩放图片
public static Bitmap zoomImg(Bitmap bm, int newWidth, int newHeight) {
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 计算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
true);
return newbm;
}
}
上一篇: 做什么网站赚钱?这个类型的网站你可以试试
下一篇: 搜狗也做百科了 给你五条实用建议