自定义设置图像比例

解决imageviw(图片)固定比例问题

思路:首先定义可以固定比例的FrameLayout,然后将ImageView放在自定义的FrameLayout上。

第一步:在values目录下建立attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
	
    <declare-styleable name="FixedScaleLayout">
        <attr name="scale" format="float" />
    </declare-styleable>

</resources>


第二步:


package com.yinongjing.widget;


import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.FrameLayout;

import com.yinongjing.xmqx.R;

public class FixedScaeLayout extends FrameLayout {
	private float scale = 1;

	public FixedScaeLayout(Context context) {
		this(context, null);

	}

	public FixedScaeLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);

	}

	public FixedScaeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
				R.styleable.FixedScaleLayout, 0, 0);

		try {
			scale = a.getFloat(R.styleable.FixedScaleLayout_scale, 1);//获取比例

		} finally {
			a.recycle();
		}
		Log.i("logutils", "scale" + scale);

	}
	
	public void setScale(float scale){
		this.scale=scale;
	}
//      设置比例
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int mode = MeasureSpec.getMode(widthMeasureSpec);
		int width = MeasureSpec.getSize(widthMeasureSpec);
		int height = (int) (scale * width);//设置高度
		heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, mode);
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		
	}

}


第三步使用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#eeeeee" > 
 <com.yinongjing.widget.FixedScaeLayout
            android:layout_width="0dp"
            android:layout_height="120dp"
            android:layout_weight="2"
            app:scale="0.5" >

            <ImageView
                android:id="@+id/ivShowImg"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"
                android:src="@drawable/img_search_fail" />
        </com.yinongjing.widget.FixedScaeLayout>
</RelativeLayout>




### 如何在 ArcGIS 中设置图像比例尺 在 ArcGIS 中设置图像比例尺是一个常见的操作,主要涉及地图显示的比例调整以及输出时的比例控制。以下是关于如何设置图像比例尺的详细说明: #### 1. 地图显示中的比例尺调整 在 ArcGIS 的地图显示中,比例尺会随着地图的缩放动态变化。比例尺的具体含义是屏幕上的一个单位长度对应实际地理空间中的距离。例如,比例尺为 1:2000 表示地图上 1 单位长度代表实际地理空间中的 2000 单位长度[^1]。 - **手动设置比例尺**: 在 ArcMap 或 ArcGIS Pro 的主界面中,可以找到“比例”文本框(通常位于工具栏的右上角)。用户可以直接输入期望的比例值,例如 1:5000,然后按回车键确认。这将使地图视图调整到指定的比例[^2]。 - **动态调整比例尺**: 当用户通过鼠标滚轮或缩放工具改变地图显示范围时,比例尺会自动更新以反映当前的缩放级别[^1]。 #### 2. 输出指定比例尺的图片 如果需要将地图以特定比例尺输出为图片文件,可以按照以下步骤操作: - **配置输出比例**: 在 ArcMap 中,选择 `File` > `Export Map` 菜单。在弹出的对话框中,确保“Use Map Georeference”选项被选中,然后在“Scale”字段中输入目标比例尺,例如 1:10000[^2]。 - **调整页面布局**: 如果需要精确控制输出图片的大小和比例,可以在页面布局模式下进行调整。通过 `Page and Print Setup` 对话框设置纸张尺寸、方向等参数,然后返回数据视图调整地图范围以匹配目标比例[^2]。 #### 3. 自定义比例尺显示 在某些情况下,可能需要自定义比例尺的显示方式。例如,在移动应用开发中使用 ArcGIS for Android 时,可以通过编程实现固定比例单位的比例尺绘制[^3]。 ```java // 示例代码:ArcGIS for Android 自定义比例尺 import com.esri.arcgisruntime.mapping.view.Graphic; import com.esri.arcgisruntime.geometry.Point; import com.esri.arcgisruntime.geometry.PolylineBuilder; public void drawScaleBar(MapView mapView, double mapScale) { // 计算比例尺的地理长度 double groundDistance = 1000; // 假设比例尺表示 1000 米 double screenLength = groundDistance / mapScale * mapView.getWidth(); // 换算为屏幕像素长度 // 创建比例尺图形 Point startPoint = new Point(10, 10); // 比例尺起点 Point endPoint = new Point(startPoint.getX() + screenLength, startPoint.getY()); // 终点 PolylineBuilder polylineBuilder = new PolylineBuilder(mapView.getSpatialReference()); polylineBuilder.addPoint(startPoint); polylineBuilder.addPoint(endPoint); Graphic scaleBarGraphic = new Graphic(polylineBuilder.toGeometry(), getSimpleLineSymbol()); mapView.getGraphicsOverlays().get(0).getGraphics().add(scaleBarGraphic); } ``` 此代码片段展示了如何根据当前地图比例绘制一个固定地理单位的比例尺[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值