this.setBounds(300,300,400,300);这句话什么意思呢?

本文解释了Java中设置窗体位置与大小的方法。通过使用this.setBounds(300,300,400,300),可以将窗体放置在屏幕坐标(300,300)处,并设定窗体宽度为400像素,高度为300像素。

this.setBounds(300,300,400,300);这句话什么意思呢?

2009-02-28 12:21 happyer10  |  分类:JAVA相关  |  浏览2390次
2009-02-28 13:23 提问者采纳
设置窗体的长宽各为:400 300
让其显示在距屏幕左上方坐标(300,300)处
//它们的计量单位是像素
这句话等效于:
this.setLocation(300, 300)
this.setSize(400, 300);
package com.tplink.libskeleton import android.animation.Animator import android.animation.ObjectAnimator import android.animation.ValueAnimator import android.graphics.Canvas import android.graphics.Color import android.graphics.ColorFilter import android.graphics.LinearGradient import android.graphics.Paint import android.graphics.Path import android.graphics.PixelFormat import android.graphics.RectF import android.graphics.Region import android.graphics.Shader import android.graphics.drawable.Drawable import android.util.Log import android.view.View import android.view.ViewGroup import android.view.animation.AccelerateDecelerateInterpolator /** * @Author: admin * @CreateDate: 2025/9/10 13:33 * @Description: */ class AnimationManager( private val targetView: View, private val config: SkeletonConfig ) { private var animator: Animator? = null private val appliedDrawables = mutableListOf<Pair<View, Drawable>>() // 记录 (view, drawable) 对 private val paint = Paint().apply { isAntiAlias = true } fun start() { stop() // 先清理旧状态 animator = createShimmerAnimator() animator?.start() } fun stop() { // 1. 停止动画 animator?.cancel() animator = null // 2. 移除所有已添加的 Drawable appliedDrawables.forEach { (view, drawable) -> try { view.overlay.remove(drawable) } catch (e: Exception) { com.tplink.omada.libutility.log.Log.e("AnimationManager", "Failed to remove overlay from view") } } appliedDrawables.clear() } private fun createShimmerAnimator(): ValueAnimator { return ValueAnimator.ofFloat(-1f, 2f).apply { duration = config.shimmerDuration repeatCount = ValueAnimator.INFINITE interpolator = AccelerateDecelerateInterpolator() addUpdateListener { anim -> val value = anim.animatedValue as Float val shader = LinearGradient( targetView.width * value, 0f, targetView.width * (value + 1), 0f, intArrayOf(Color.TRANSPARENT, Color.parseColor("#EEEEEE"), Color.TRANSPARENT), floatArrayOf(0f, 0.5f, 1f), Shader.TileMode.CLAMP ) paint.shader = shader // 触发所有 shimmer drawable 重绘 appliedDrawables.forEach { (_, drawable) -> drawable.invalidateSelf() } } }.also { animator -> // 创建 shimmer drawables 并添加到 overlays fun recursivelyApplyShimmer(view: View) { com.tplink.omada.libutility.log.Log.d( "AnimationManager", "Applying shimmer to: ${view.javaClass.simpleName}, ID: ${view.id}" ) if (view is ViewGroup) { for (i in 0 until view.childCount) { recursivelyApplyShimmer(view.getChildAt(i)) } } else { // 确保 view 尺寸有效 if (view.width <= 0 || view.height <= 0) return val cornerRadius = config.cornerRadius.coerceAtLeast(0f) val shimmerDrawable = object : Drawable() { private var clipPath: Path? = null override fun draw(canvas: Canvas) { val bounds = this.bounds if (bounds.isEmpty || paint.shader == null) return if (cornerRadius > 0f) { // 创建圆角矩形路径 val path = Path().apply { addRoundRect( RectF(bounds), cornerRadius, cornerRadius, Path.Direction.CW ) } // 裁剪画布:只允许在路径内绘制 canvas.clipPath(path, Region.Op.INTERSECT) } // 绘制渐变光带 canvas.drawRect(bounds, paint) } override fun setAlpha(alpha: Int) {} override fun setColorFilter(colorFilter: ColorFilter?) {} @Deprecated("Deprecated in Java") override fun getOpacity(): Int = PixelFormat.TRANSLUCENT }.apply { setBounds(0, 0, view.width, view.height) view.overlay.add(this) } appliedDrawables.add(view to shimmerDrawable) } } // 开始递归添加 shimmer 效果 recursivelyApplyShimmer(targetView) } } private fun createPulseAnimator(): ObjectAnimator { return ObjectAnimator.ofFloat(targetView, View.ALPHA, 1f, 0.5f, 1f).apply { duration = config.shimmerDuration repeatCount = ValueAnimator.INFINITE interpolator = AccelerateDecelerateInterpolator() } } }解释这个代码,写成简单点的,不用glamada
11-20
我问你,怎么控制这个光带的速度!class AnimationManager( private val targetView: View, private val config: SkeletonConfig ) { private var animator: ValueAnimator? = null private val appliedDrawables = mutableListOf<Pair<View, Drawable>>() private val paint = Paint().apply { isAntiAlias = true } /** * 启动 Shimmer 动画 * 若已有动画运行,则先停止再启动新动画 */ fun start() { stop() // 清理旧状态 animator = createShimmerAnimator() animator?.start() } /** * 停止当前动画并清理所有已添加的 overlay drawable */ fun stop() { animator?.cancel() animator = null appliedDrawables.forEach { (view, drawable) -> try { view.overlay.remove(drawable) } catch (e: Exception) { Log.e("AnimationManager", "Failed to remove overlay from view") } } appliedDrawables.clear() } /** * 创建控制 shimmer 动画的 ValueAnimator * * 动画范围从 -1f 到 2f,用于驱动 LinearGradient 水平移动,形成“扫光”效果 */ private fun createShimmerAnimator(): ValueAnimator { val animator = ValueAnimator.ofFloat(-1f, 2f).apply { duration = config.shimmerDuration repeatCount = ValueAnimator.INFINITE interpolator = AccelerateDecelerateInterpolator() addUpdateListener { animation -> val fraction = animation.animatedValue as Float updateShader(fraction) invalidateAllDrawables() } } applyShimmerToTargetView() return animator } /** * 更新渐变着色器(Shader),实现移动的高亮光效 */ private fun updateShader(offsetFraction: Float) { val width = targetView.width.toFloat() if (width <= 0f) return val startX = width * offsetFraction val endX = width * (offsetFraction + 1) val colors = intArrayOf(Color.TRANSPARENT, config.shimmerColor, Color.TRANSPARENT) val positions = floatArrayOf(0f, 0.5f, 1f) val shader = LinearGradient( startX, 0f, endX, 0f, colors, positions, Shader.TileMode.CLAMP ) paint.shader = shader } /** * 触发所有 shimmer drawable 重绘,使其显示最新 shader 效果 */ private fun invalidateAllDrawables() { appliedDrawables.forEach { (_, drawable) -> drawable.invalidateSelf() } } /** * 遍历目标视图及其子视图,为每个非 ViewGroup 的叶子视图创建并添加 shimmer drawable */ private fun applyShimmerToTargetView() { recursivelyApplyShimmer(targetView) } private fun recursivelyApplyShimmer(view: View) { if (view is ViewGroup) { for (i in 0 until view.childCount) { recursivelyApplyShimmer(view.getChildAt(i)) } } else { // 只有当视图具有有效尺寸时才应用 shimmer if (view.width <= 0 || view.height <= 0) return if (view.tag == "ignore_ani") return val shimmerDrawable = createShimmerDrawable(view) view.overlay.add(shimmerDrawable) appliedDrawables.add(view to shimmerDrawable) } } /** * 为指定视图创建一个自定义 Drawable,用于显示带有圆角支持的 shimmer 效果 */ private fun createShimmerDrawable(view: View): Drawable { val cornerRadius = config.cornerRadius.coerceAtLeast(0f) return object : Drawable() { override fun draw(canvas: Canvas) { val bounds = this.bounds if (bounds.isEmpty || paint.shader == null) return if (cornerRadius > 0f) { val rectF = RectF(bounds) val path = Path().apply { addRoundRect(rectF, cornerRadius, cornerRadius, Path.Direction.CW) } canvas.save() canvas.clipPath(path) canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint) canvas.restore() } else { canvas.drawRect(bounds, paint) } } /** * 此处不处理 Alpha 设置,因为 shimmer 效果由 shader 控制透明度 */ override fun setAlpha(alpha: Int) { // 忽略:颜色透明度由 LinearGradient 决定 } /** * 不支持颜色过滤器 */ override fun setColorFilter(colorFilter: ColorFilter?) { // 忽略:shimmer 是纯图形动画,不需要滤镜 } /** * 返回半透明像素格式,表示此 Drawable 支持透明度 */ override fun getOpacity(): Int = PixelFormat.TRANSLUCENT }.apply { setBounds(0, 0, view.width, view.height) } } }
最新发布
12-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值