java rect_java rect

.点击软键盘自带的收起按钮(软键盘收起)import android.graphics.Rect;

import android.view.View;

import android.view.ViewTreeObserver;

import java.util.LinkedList;

import java.util.List;

public class SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener {

public interface SoftKeyboardStateListener {

void onSoftKeyboardOpened(int keyboardHeightInPx);

void onSoftKeyboardClosed();

}

private final List listeners = new LinkedList();

private final View activityRootView;

private int lastSoftKeyboardHeightInPx;

private boolean isSoftKeyboardOpened;

public SoftKeyboardStateHelper(View activityRootView) {

this(activityRootView, false);

}

public SoftKeyboardStateHelper(View activityRootView, boolean isSoftKeyboardOpened) {

this.activityRootView = activityRootView;

this.isSoftKeyboardOpened = isSoftKeyboardOpened;

activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);

}

@Override

public void onGlobalLayout() {

final Rect r = new Rect();

//r will be populated with the coordinates of your view that area still visible.

activityRootView.getWindowVisibleDisplayFrame(r);

final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);

if (!isSoftKeyboardOpened && heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...

isSoftKeyboardOpened = true;

notifyOnSoftKeyboardOpened(heightDiff);

} else if (isSoftKeyboardOpened && heightDiff < 100) {

isSoftKeyboardOpened = false;

notifyOnSoftKeyboardClosed();

}

}

public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {

this.isSoftKeyboardOpened = isSoftKeyboardOpened;

}

public boolean isSoftKeyboardOpened() {

return isSoftKeyboardOpened;

}

/**

* Default value is zero (0)

* @return last saved keyboard height in px

*/

public int getLastSoftKeyboardHeightInPx() {

return lastSoftKeyboardHeightInPx;

}

public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {

listeners.add(listener);

}

public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) {

listeners.remove(listener);

}

private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {

this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;

for (SoftKeyboardStateListener listener : listeners) {

if (listener != null) {

listener.onSoftKeyboardOpened(keyboardHeightInPx);

}

}

}

private void notifyOnSoftKeyboardClosed() {

for (SoftKeyboardStateListener listener : listeners) {

if (listener != null) {

listener.onSoftKeyboardClosed();

}

}

}

}代码的使用:final SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(findViewById(R.id.activity_main_layout);

softKeyboardStateHelper.addSoftKeyboardStateListener(...);

// then just handle callbacks

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java2D 是 Java 平台提供的一个 2D 图形 API,可以用来创建各种图形、绘制图像、处理颜色、应用纹理等等。下面是一个简单的例子,展示如何使用 Java2D 创建一个简单的图形。 ```java import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.JFrame; import javax.swing.JPanel; public class MyPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; // 设置背景颜色 g2d.setBackground(Color.WHITE); // 设置画笔颜色 g2d.setColor(Color.BLACK); // 创建一个矩形 Rectangle rect = new Rectangle(50, 50, 100, 100); // 填充矩形 g2d.fill(rect); } public static void main(String[] args) { JFrame frame = new JFrame("Java2D Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 200); MyPanel panel = new MyPanel(); frame.add(panel); frame.setVisible(true); } } ``` 在上面的代码中,我们继承了 JPanel 类,并重写了它的 paintComponent 方法。在该方法中,我们首先调用了父类的 paintComponent 方法,然后获取 Graphics2D 对象,设置了背景颜色和画笔颜色,并创建了一个矩形,最后使用 fill 方法填充矩形。 在 main 方法中,我们创建了一个 JFrame 对象,设置了标题和大小,创建了一个 MyPanel 对象,并将它添加到 JFrame 中,最后设置 JFrame 可见。运行程序,你会看到一个黑色的矩形在白色的背景上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值