许久以前,在我还是初中的时候,有过一个当网络小说家的梦想,谁知到如今,却成一个程序员。享受生活,享受现在,做一个写博客的程序员,也算是了却当初的半个梦想。

                                                                                                                                                                                             ---萧洛

 本博客分三部分讲解

设置大小

    2.在Java文件中设置imageview的位置

控制imageview的大小

 

1.在Java文件中添加imageview

    1.1 布局文件  

 

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.imageview_location_largeandnarrow.MainActivity">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fadingEdge="none">
       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:id="@+id/linearlayout"
           android:orientation="vertical">
           <LinearLayout
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:orientation="vertical">
               <Button
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:id="@+id/add_imageview"
                   android:text="addImageview"
                   android:textAllCaps="false"
                   android:layout_gravity="center"
                  />
           </LinearLayout>

           <LinearLayout
               android:layout_width="match_parent"
               android:layout_height="wrap_content">
               <RelativeLayout
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   android:id="@+id/relativelayout_addimageview">
               </RelativeLayout>
           </LinearLayout>
       </LinearLayout>

    </ScrollView>

</android.support.constraint.ConstraintLayout>

需要设置LinearLayout方向为垂直才会生效。

 

 

    1.2 Java文件

 

package com.example.administrator.imageview_location_largeandnarrow;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import static com.example.administrator.imageview_location_largeandnarrow.R.color.colorAccent;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button add_imageview = findViewById(R.id.add_imageview);
        final RelativeLayout relativeLayout_add_imageview = findViewById(R.id.relativelayout_addimageview);
        add_imageview.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("ResourceAsColor")
            @Override
            public void onClick(View v) {
                ImageView imageView = new ImageView(MainActivity.this);
                imageView.setImageResource(R.drawable.sunwukong);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(400,
                        400);//两个400分别为添加图片的大小
                imageView.setLayoutParams(params);
                imageView.setBackgroundColor(colorAccent);
                relativeLayout_add_imageview.addView(imageView);
            }
        });
    }
}

imageview控件的实际大小。

 

 

 

    1.3 仿真结果

                               

android studio的imageview androidstudio的imageview的位置怎么设置_单方向放大和缩小

    

 

2.在Java文件中设置图片的位置

2.1 Java文件(布局文件无需改变)

 

package com.example.administrator.imageview_location_largeandnarrow;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import static com.example.administrator.imageview_location_largeandnarrow.R.color.colorAccent;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button add_imageview = findViewById(R.id.add_imageview);
        final RelativeLayout relativeLayout_add_imageview = findViewById(R.id.relativelayout_addimageview);
        add_imageview.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("ResourceAsColor")
            @Override
            public void onClick(View v) {
                ImageView imageView = new ImageView(MainActivity.this);
                imageView.setImageResource(R.drawable.sunwukong);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(400,
                        400);
                imageView.setLayoutParams(params);
                imageView.setBackgroundColor(colorAccent);
                ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(imageView.getLayoutParams());
                margin.leftMargin=40;
                margin.topMargin=100;
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin);
                layoutParams.height = 400;//设置图片的高度
                layoutParams.width = 400; //设置图片的宽度
                imageView.setLayoutParams(layoutParams);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);//使图片充满控件大小
                relativeLayout_add_imageview.addView(imageView);
            }
        });
    }
}

图片充满控件。

    2.2 仿真结果

    

android studio的imageview androidstudio的imageview的位置怎么设置_单方向放大和缩小

   

android studio的imageview androidstudio的imageview的位置怎么设置_设置位置_03

     还是可以很轻易看出结果的变化的。

 

3.在Java文件中控件图片的单方向的放大

     这个才是本文的重点,也是目前网上,很难找到参考资料的。

    3.1 Java文件

 

package com.example.administrator.imageview_location_largeandnarrow;

import android.annotation.SuppressLint;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import static com.example.administrator.imageview_location_largeandnarrow.R.color.colorAccent;

public class MainActivity extends AppCompatActivity {

    private int num=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button add_imageview = findViewById(R.id.add_imageview);
        final RelativeLayout relativeLayout_add_imageview = findViewById(R.id.relativelayout_addimageview);
        add_imageview.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("ResourceAsColor")
            @Override
            public void onClick(View v) {
                int screenWidth = getScreenWidth();
                ImageView imageView = new ImageView(MainActivity.this);
                imageView.setImageResource(R.drawable.sunwukong);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        400);
                imageView.setLayoutParams(params);
                imageView.setBackgroundColor(colorAccent);
                ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(imageView.getLayoutParams());
                margin.leftMargin=40;
                margin.topMargin=10+num;
                margin.rightMargin=screenWidth-400-margin.leftMargin-num;
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin);
                imageView.setLayoutParams(layoutParams);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);//使图片充满控件大小
                relativeLayout_add_imageview.addView(imageView);
                num+=100;
            }
        });
    }
    public int getScreenWidth() {
        Point point = new Point();
        getWindowManager().getDefaultDisplay().getSize(point);
        return point.x;
    }

}

 

 

    好啦,终于到了最激动人心的时刻! 上述代码中,设置的是将imageview横向放大,所以将固定长度400的宽度,设置成了wrap-content。要放大自然不能固定大小!上文中我们说到,控制控件在父布局中的位置的时候,不需要设置右和下的距离,1是因为既然设置左和上的距离就能控制位置,就无需多次一举,2是设置错了的话,会改变图片的大小。父控件总的宽度-控件到父控件左边的距离-右边的距离=控件的宽度!! 对的,就是这么简单,利用这个原理就可以轻松实现了图片的单方向放大。至此,相信你也应该明白,为啥要设置图片充满布局了吧!

    3.2 仿真结果    

                 

android studio的imageview androidstudio的imageview的位置怎么设置_控件_04

    好啦,至此为止,第一篇博客算是写完了。