秒懂imageView的scaleType属性

秒懂imageView的scaleType属性,第1张

1、比如imageView,有一个getLayout方法,获得的layout在强转类型到LinearLayout或者其他,然后再设定margin什么的。

2、我们平常可以直接在xml里设置margin,如:

Xml代码 <ImageView android:layout_margin="5dip" android:src="@drawable/image" />

但是有些情况下,需要在java代码里来写,可是View本身没有setMargin方法,怎么办呢?

通过查阅android api,我们发现androidviewViewGroupMarginLayoutParams有个方法

setMargins(left, top, right, bottom)。

其直接的子类有: FrameLayoutLayoutParams, LinearLayoutLayoutParams and RelativeLayoutLayoutParams。

自定义一个jpanel用于展示image

/

  

 /

package orgdemo;

import javaawtGraphics;

import javaawtGraphics2D;

import javaawtImage;

import javautilHashMap;

import javautilMap;

import javaxswingJPanel;

/

  @ClassName: ImageView

  @Description: TODO(这里用一句话描述这个类的作用)

  @author Btboy1978

  @date 2017年6月12日 下午9:46:13 

 /

public class ImageView extends JPanel {

private Map<Integer, Image> images;

private int currentIndex;

public ImageView() {

super();

images = new HashMap<Integer, Image>();

currentIndex = 0;

}

/

  (non-Javadoc)

  

  @see javaxswingJComponent#paint(javaawtGraphics)

 /

@Override

public void paint(Graphics g) {

gclearRect(0, 0, WIDTH, HEIGHT);

Graphics2D g2 = (Graphics2D) g;

g2drawImage(imagesget(currentIndex), 0, 0, this);

}

public void showNext() {

if (imagessize() != 0) {

if (currentIndex != imagessize() - 1) {

currentIndex++;

}

}

thisrepaint();

}

public void showUp() {

if (imagessize() != 0) {

if (currentIndex != 0) {

currentIndex--;

}

}

thisrepaint();

}

public void addImage(Image image){

imagesput(imagessize(), image);

Systemoutprintln(imagessize());

}

}

测试方法

/

  

 /

package orgdemo;

import javaawtBorderLayout;

import javaawtEventQueue;

import javaxswingJFrame;

import javaxswingJPanel;

import javaxswingborderEmptyBorder;

import javaximageioImageIO;

import javaxswingJButton;

import javaawteventActionListener;

import javaioFile;

import javaioIOException;

import javaawteventActionEvent;

 @ClassName: ImageViewTest 

 @Description: TODO(这里用一句话描述这个类的作用) 

 @author Btboy1978

 @date 2017年6月12日 下午9:59:42   

/

public class ImageViewTest extends JFrame {

private JPanel contentPane;

private ImageView imageView;

/

  Launch the application

 /

public static void main(String[] args) {

EventQueueinvokeLater(new Runnable() {

public void run() {

try {

ImageViewTest frame = new ImageViewTest();

framesetVisible(true);

} catch (Exception e) {

eprintStackTrace();

}

}

});

}

/

  Create the frame

 /

public ImageViewTest() {

setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

setBounds(100, 100, 450, 393);

contentPane = new JPanel();

contentPanesetBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPanesetLayout(null);

imageView = new ImageView();

imageViewsetBounds(10, 10, 414, 284);

try {

imageViewaddImage(ImageIOread(new File("images/1jpg")));

imageViewaddImage(ImageIOread(new File("images/2jpg")));

imageViewaddImage(ImageIOread(new File("images/3jpg")));

imageViewaddImage(ImageIOread(new File("images/4jpg")));

} catch (IOException e1) {

// TODO Auto-generated catch block

e1printStackTrace();

}

contentPaneadd(imageView);

JButton btnNewButton = new JButton("UP");

btnNewButtonaddActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

imageViewshowUp();

}

});

btnNewButtonsetBounds(81, 304, 117, 41);

contentPaneadd(btnNewButton);

JButton button = new JButton("NEXT");

buttonaddActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

imageViewshowNext();

}

});

buttonsetBounds(228, 304, 117, 41);

contentPaneadd(button);

}

}

import javaawtBorderLayout;

import javaawtGraphics;

import javaawtGridLayout;

import javaawteventActionEvent;

import javaawteventActionListener;

import javaioFile;

import javautilArrayList;

import javaxswingImageIcon;

import javaxswingJButton;

import javaxswingJFileChooser;

import javaxswingJFrame;

import javaxswingJMenu;

import javaxswingJMenuBar;

import javaxswingJMenuItem;

import javaxswingJPanel;

public class ImageGallery extends JFrame implements ActionListener {

public static final int G_WIDTH = 400, G_HEIGHT = 620;

public static final int L_WIDTH = 1200, L_HEIGHT = 520;

public static final int MAX_R = 3, MAX_C = 2;

public static final int GRID = 1, LARGE = 2;

private JFileChooser chooser;

private JMenuBar toolBar;

private JMenu menu;

private JMenuItem open;

private ArrayList<String> paths;

private JPanel showingPanel, buttonPanel;

private int page = 1;

private int count = 0;

private int showType = GRID;

private int pageMax;

private JButton last, next, large, grid;

public ImageGallery() {

thissetTitle("Image gallery 01");

thissetBounds(100, 100, G_WIDTH, G_HEIGHT);

thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

toolBar = new JMenuBar();

chooser = new JFileChooser();

choosersetCurrentDirectory(new javaioFile(""));

choosersetFileSelectionMode(JFileChooserDIRECTORIES_ONLY);

menu = new JMenu("File");

toolBaradd(menu);

open = new JMenuItem("open");

openaddActionListener(this);

menuadd(open);

paths = new ArrayList<String>();

thissetJMenuBar(toolBar);

showingPanel = new JPanel();

buttonPanel = new JPanel();

last = new JButton("last");

lastaddActionListener(this);

next = new JButton("next");

nextaddActionListener(this);

large = new JButton("large");

largeaddActionListener(this);

grid = new JButton("grid");

gridaddActionListener(this);

buttonPaneladd(last);

buttonPaneladd(large);

buttonPaneladd(grid);

buttonPaneladd(next);

thisadd(buttonPanel, BorderLayoutSOUTH);

thisadd(showingPanel);

thissetVisible(true);

}

public void doOpen() {

int result = choosershowOpenDialog(null);

if (result == JFileChooserAPPROVE_OPTION) {

String directory = choosergetSelectedFile()getPath();

thisgetAllImage(directory);

for (String s : thispaths) {

Systemoutprintln(s);

}

thisshowingImage();

}

}

public void getAllImage(String directory) {

pathsclear();

page = 1;

count = 0;

File file = new File(directory);

if (fileisDirectory()) {

String[] list = filelist();

for (String s : list) {

String path = directory + "\\" + s;

File temp = new File(path);

if (!tempisDirectory()) {

thispathsadd(path);

}

}

}

}

public void changePage(int page) {

if (!pathsisEmpty()) {

switch (showType) {

case GRID:

thispage += page;

if (thispage == 0) {

thispage = 1;

}

if (thispage > pageMax) {

thispage = pageMax;

}

break;

case LARGE:

thiscount += page;

if (count < 0) {

count = 0;

}

if (count > pathssize() - 1) {

count = pathssize() - 1;

}

break;

}

thisshowingImage();

}

}

public void showingImage() {

thisremove(showingPanel);

showingPanel = new JPanel();

int size = pathssize();

int max = MAX_R MAX_C;

pageMax = (size % max == 0 size / max : size / max + 1);

switch (showType) {

case GRID:

thissetBounds(100, 100, G_WIDTH, G_HEIGHT);

showingPanelsetLayout(new GridLayout(MAX_R, MAX_C - 1));

int i = (page - 1) max;

int j = page max;

for (; i < j && i < size; i++) {

String path = pathsget(i);

showingPaneladd(new ImagePanel(path));

}

break;

case LARGE:

int left = count - 1;

int right = count + 1;

thissetBounds(100, 100, L_WIDTH, L_HEIGHT);

showingPanelsetLayout(new GridLayout(1, 2));

if (left >= 0) {

showingPaneladd(new ImagePanel(pathsget(left)));

} else {

showingPaneladd(new ImagePanel(""));

}

showingPaneladd(new ImagePanel(pathsget(count)));

if (right < size) {

showingPaneladd(new ImagePanel(pathsget(right)));

} else {

showingPaneladd(new ImagePanel(""));

}

break;

}

thisadd(showingPanel, BorderLayoutCENTER);

showingPanelupdateUI();

}

/

@param args

/

public static void main(String[] args) {

// TODO Auto-generated method stub

new ImageGallery();

}

class ImagePanel extends JPanel {

private ImageIcon image;

ImagePanel(String path) {

image = new ImageIcon(path);

}

public ImageIcon getImageIcon() {

return thisimage;

}

@Override

protected void paintComponent(Graphics g) {

// TODO Auto-generated method stub

gdrawImage(imagegetImage(), 0, 0, thisgetWidth(), this

getHeight(), this);

}

}

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

String key = egetActionCommand();

Systemoutprintln(key);

if (keyequals("open")) {

doOpen();

} else if (keyendsWith("last")) {

changePage(-1);

} else if (keyendsWith("next")) {

changePage(1);

} else if (keyendsWith("large")) {

if (!pathsisEmpty()) {

count =0;

showType = LARGE;

showingImage();

}

} else if (keyendsWith("grid")) {

if (!pathsisEmpty()) {

page = 1;

showType = GRID;

showingImage();

}

}

}

}

以上就是关于秒懂imageView的scaleType属性全部的内容,包括:秒懂imageView的scaleType属性、北大青鸟java培训:北大青鸟校区IOS课程介绍、Android如何在java代码中设置margin等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9872604.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-02
下一篇2023-05-02

发表评论

登录后才能评论

评论列表(0条)

    保存