项目中需要用到在代码中动态调整图片的位置和设置图片大小,能自动缩放图片,用ImageView控件,具体做法如下:

1、布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<ImageView
android:id="@+id/image01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/aabb" />
</RelativeLayout>

2、java代码

public class MainActivity extends Activity {
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView)findViewById(R.id.image01);
//设置图片的位置
MarginLayoutParams margin9 = new MarginLayoutParams(
image.getLayoutParams());
margin9.setMargins(400, 10, 0, 0);//在左边距400像素,顶边距10像素的位置显示图片
RelativeLayout.LayoutParams layoutParams9 = new RelativeLayout.LayoutParams(margin9);
layoutParams9.height = 600;//设置图片的高度
layoutParams9.width = 800; //设置图片的宽度
image.setLayoutParams(layoutParams9);
}
}

  

3、效果

Android代码中动态设置图片的大小(自动缩放),位置的更多相关文章

  1. vue中动态设置echarts画布大小

    document.getElementById('news-shopPagechart').style.height = this.heightpx2+'px'; //heightpx2定义在data ...

  2. [K/3Cloud] 如何代码中动态设置当前活动页签

    this.GetControl<TabControl>(key).SelectedIndex=目标Index Ps:如下方式隐藏页签: this.View.GetControl(" ...

  3. 在响应式布局中,碰到图片不会自动缩放,因此需要一段js脚本

    <script> (function (doc, win) { var docEl = doc.documentElement, resizeEvt = 'orientationchang ...

  4. Android代码中设置字体大小,字体颜色,显示两种颜色.倒计时效果

    Android代码中设置字体大小,字体颜色,显示两种颜色 在xml文件中字体大小用的像素 <TextView android:id="@+id/uppaid_time" an ...

  5. xamarin android 在代码中如何设置文本颜色

    xamarin android 在代码中如何设置文本颜色 TextView v = FindViewById<TextView>(Android.Resource.Id.Message); ...

  6. 周记4——vue中动态添加图片无效、build上线后background-image路径问题

    又是一个周五,又一周要过去了...很开心,这周遇到了vue中的一个比较常见的坑,网上随便一搜就有了很多解决方案...“幸运”的是,我选了一个带坑的方案...所以我觉得有必要记录一下这个“坑中坑”... ...

  7. Quartz在Spring中动态设置cronExpression (spring设置动态定时任务)

    什么是动态定时任务:是由客户制定生成的,服务端只知道该去执行什么任务,但任务的定时是不确定的(是由客户制定).      这样总不能修改配置文件每定制个定时任务就增加一个trigger吧,即便允许客户 ...

  8. 解决iOS中 tabBarItem设置图片(image+title切图在一起)时造成的图片向上偏移

    解决iOS中 tabBarItem设置图片(image+title切图在一起)时造成的图片向上偏移 解决办法1:设置tabBarItem的imageInsets属性 代码示例: childContro ...

  9. Quartz在Spring中动态设置cronExpression

    什么是动态定时任务:是由客户制定生成的,服务端只知道该去执行什么任务,但任务的定时是不确定的(是由客户制定). 这样总不能修改配置文件每定制个定时任务就增加一个trigger吧,即便允许客户修改配置文 ...

随机推荐

  1. HDU1632+半平面交

    模板题 题意:给定两个凸多边形,求出合并后的面积,这个合并后的面积不包括重叠部分. #include<stdio.h> #include<string.h> #include& ...

  2. http://www.linuxidc.com/Linux/2015-02/114265.htm

    http://www.linuxidc.com/Linux/2015-02/114265.htm

  3. c helloworld on zynq

    A linux os runs on zynq board. I want ro run a hello world c program on it. I linked zynq board to a ...

  4. github Git 原理简介

    由于Git是一个DVCS(Distributed Version Control System,分布式版本控制系统),不同于传统的CVS/SVN版本系统那样必须由一个中央服务器来管理所有的版本记录,它 ...

  5. Linq的延迟

    书名:LINQ: The Future of Data Access in C# 3.0 Learn LINQ and the C# 3.0 Features That Support It http ...

  6. AOP 实现的原理简析

    AOP简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编程(也叫面向方面),可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能 ...

  7. bzoj2527

    首先环可以变成链来处理,对于l>r的情况就是修改区间[1,r],[l,mx]然后不难想到整体二分,二分答案k,然后算1~k场流星雨对国家的贡献然后判定将国家划分变成子问题解决,没什么难的终于不是 ...

  8. Linux Kernel ‘exitcode_proc_write()’函数本地缓冲区溢出漏洞

    漏洞名称: Linux Kernel ‘exitcode_proc_write()’函数本地缓冲区溢出漏洞 CNNVD编号: CNNVD-201311-061 发布时间: 2013-11-07 更新时 ...

  9. SharePoint 2010 WSP包部署过程中究竟发生什么?

    转:http://www.xue163.com/158/6/1585365.html 在SharePoint 2010中,我们可以使用Visual Studio 2010轻松创建WSP包来安装Web ...

  10. STL find() ,还是挺重要的

    template<class InputIterator, class T> InputIterator find (InputIterator first, InputIterator ...