- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在绘画事件中,因为我希望能够控制点大小颜色和更多属性。
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
public partial class LoadingLabel : UserControl
{
public LoadingLabel()
{
InitializeComponent();
}
private void LoadingLabel_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.FillEllipse(Brushes.Red, 1, 1, 20, 20);
Thread.Sleep(1);
e.Graphics.FillEllipse(Brushes.Red, 1, 1, 0, 0);
Thread.Sleep(1);
}
}
我首先尝试制作一个简单的点,该点在一段时间后消失,然后再次显示,但它不起作用,我看到一个红色的静止点(点)。
稍后当它起作用时,我想制作 3 个点的动画,就像加载动画一样。
这是我尝试过的:
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
public partial class LoadingLabel : UserControl
{
private bool animate = false;
public LoadingLabel()
{
InitializeComponent();
timer1.Enabled = true;
}
private void LoadingLabel_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
if (animate == false)
{
e.Graphics.FillEllipse(Brushes.Red, 1, 1, 20, 20);
}
else
{
e.Graphics.FillEllipse(Brushes.Red, 5, 1, 20, 20);
}
}
int count = 0;
private void timer1_Tick(object sender, EventArgs e)
{
count++;
if(count == 10 && animate == false)
{
animate = true;
}
if(count == 20 && animate)
{
animate = false;
count = 0;
}
this.Invalidate();
}
}
结果是第一个点绘制,然后是第二个点绘制,但第一个点消失了:
看起来该点向右移动,然后又回到左侧。
但我想要3点的加载效果。并且不移动点。
这适用于 3 点,但对于 3 点来说看起来太复杂了。如果我想要 100 分?
也许我应该在绘制事件中使用循环?
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
public partial class LoadingLabel : UserControl
{
private int numofpoints = 0;
public LoadingLabel()
{
InitializeComponent();
timer1.Enabled = true;
}
private void LoadingLabel_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
if(numofpoints == 0)
{
e.Graphics.FillEllipse(Brushes.Red, 1, 1, 20, 20);
}
if(numofpoints == 1)
{
e.Graphics.FillEllipse(Brushes.Red, 5, 1, 20, 20);
}
if(numofpoints == 2)
{
e.Graphics.FillEllipse(Brushes.Red, 10, 1, 20, 20);
}
}
int count = 0;
private void timer1_Tick(object sender, EventArgs e)
{
count++;
if(count == 10)
{
numofpoints = 0;
}
if(count == 20)
{
numofpoints = 1;
}
if(count == 30)
{
numofpoints = 2;
count = 0;
}
this.Invalidate();
}
}
我尝试过的另一个更新:
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
public partial class LoadingLabel : UserControl
{
private List<PointF> points = new List<PointF>();
public LoadingLabel()
{
InitializeComponent();
points.Add(new PointF(0, 0));
timer1.Enabled = true;
}
private void LoadingLabel_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
for (int i = 0; i < points.Count; i++)
{
e.Graphics.FillEllipse(Brushes.Red, points[i].X, points[i].Y, 20, 20);
}
}
int count = 0;
private void timer1_Tick(object sender, EventArgs e)
{
count++;
if (count < 3)
{
points.Add(new PointF(count * 20, 0));
//points = new List<PointF>();
}
//this.Invalidate();
}
}
如果我在勾选事件中创建实例,它将不会绘制任何内容。如果我使用无效线,它会使点像闪烁一样。我想要的是创建一个加载效果动画。
现在代码的结果仍然是 3 点,我想像链接中那样为它们设置动画。
类似这样的事情:
最佳答案
由于,根据您发布的图像,您想要为一系列点设置动画,其中只有事件的点会改变颜色,因此您的用户控件可以定义允许指定点数量的属性、点的颜色和事件点的颜色。
计时器可用于更改当前事件的点,因此绘制过程知道何时更改其中一个点的颜色。
当指定的点数发生变化时,UserControl 会自动调整大小。
此外,首次创建 UserControl 时,它会设置其 MinimumSize
,因此点始终可见。
您可以扩展此模板,添加更多功能。
请注意 UserControl 构造函数中的这些行:
components = new Container();
dotsTimer = new Timer(components) { ... };
这指示定时器组件将自身添加到父容器的组件中,因此当父容器被释放时,定时器也被释放,并且其事件处理程序被删除。
设置 DoubleBuffered = true;
可避免绘制点时闪烁。
调用 Start()
方法来启动动画,并调用 Stop()
方法来停止动画。
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
public partial class LoadingLabel : UserControl {
private int m_NumberOfDots = 5;
private Color m_DotColor = Color.Cyan;
private Color m_DotActiveColor = Color.Blue;
private float dotSize = 20.0f;
private float dotSpacing = 20.0f;
private int currentDot = 0;
private Timer dotsTimer = null;
public LoadingLabel()
{
InitializeComponent();
components = new Container();
dotsTimer = new Timer(components) { Interval = 200 };
dotsTimer.Tick += DotsTimer_Tick;
DoubleBuffered = true;
Padding = new Padding(5);
}
[DefaultValue(5)]
public int NumberOfDots {
get => m_NumberOfDots;
set {
value = Math.Max(3, Math.Min(value, 7));
if (m_NumberOfDots != value) {
m_NumberOfDots = value;
bool running = dotsTimer.Enabled;
Stop();
SetMinSize();
if (running) Start();
}
}
}
[DefaultValue(typeof(Color), "Cyan")]
public Color DotColor {
get => m_DotColor;
set {
m_DotColor = value;
Invalidate();
}
}
[DefaultValue(typeof(Color), "Blue")]
public Color DotActiveColor {
get => m_DotActiveColor;
set {
m_DotActiveColor = value;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e) {
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
for (int dot = 0; dot < m_NumberOfDots; dot++) {
var color = dot == currentDot ? DotActiveColor : DotColor;
var pos = Padding.Left + (dotSize + dotSpacing) * dot;
using (var brush = new SolidBrush(color)) {
e.Graphics.FillEllipse(brush, pos, Padding.Top, dotSize, dotSize);
}
}
base.OnPaint(e);
}
protected override void OnHandleCreated(EventArgs e) {
base.OnHandleCreated(e);
SetMinSize();
}
protected override void OnHandleDestroyed(EventArgs e) {
Stop();
base.OnHandleDestroyed(e);
}
private void DotsTimer_Tick(object sender, EventArgs e) {
currentDot += 1;
currentDot %= m_NumberOfDots;
Invalidate();
}
public void Start() => dotsTimer.Start();
public void Stop() {
dotsTimer.Stop();
currentDot = 0;
Invalidate();
}
private void SetMinSize() {
var width = Padding.Left + Padding.Right +
(dotSize * m_NumberOfDots) + (dotSpacing * (m_NumberOfDots - 1)) + 1;
var height = Padding.Top + Padding.Bottom + dotSize + 1;
MinimumSize = new Size((int)width, (int)height);
Size = MinimumSize;
}
}
它是这样工作的:
按需,this is the PasteBin用于选择颜色的自定义组合框控件的一部分。
关于c# - 如何在 UserControl Paint 事件中对点进行动画处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73788268/
我正在尝试以编程方式创建位图,但发现绘制带阴影的 Rect 会忽略传入的颜色 arg。我已将事情简化为这种情况 - 代码只是绘制了一个旋转的蓝色方 block ,它应该有一个灰色的影子,但影子总是蓝色
我可以轻松使用为 Paint.NET 制作的插件吗?在我自己的 C# 应用程序中(假设我遵守插件许可)? 我的目标是使某些效果自动化,Paint.NET 似乎有很多。 最佳答案 Paint.NET 以
我很好奇这些方法的作用以及如何使用它们,因为除了 Javadocs 中的单句描述之外似乎没有任何细节: setStrokeJoin setStrokeMiter 有人有一些示例代码或好的描述吗? 最佳
这个问题在这里已经有了答案: Error printing image in PyQt (1 个回答) PyQt print QWidget (1 个回答) 关闭 2 年前。 我试图在 QPixmap
我对这个感到困惑。我尝试按照一些人的建议将 QPainter 移动到它自己的 def ,但它给出了完全相同的错误。这是我创建的 def。 def PaintButtons(self): sol
我遇到这个问题,当我执行 repaint() 时,类中的 paint() 或 update() 方法没有被调用。这是代码: public class BufferedDisplay extends C
我一直在阅读关于 chrome 中的关键渲染路径 here和 here .我对这些资源的理解是“合成”步骤发生在主线程之外,并且依赖于之前的“绘制”步骤,该步骤生成要合成在一起的光栅化图层。 但是,当
在我的应用程序中,用户之一有时会出错并且应用程序崩溃。用户向我发送日志: java.lang.IndexOutOfBoundsException at android.graphics.Paint.
我正在编写简单的画图程序,您可以在其中通过拖动鼠标来绘制任何您想要的东西。您可以更改画笔的颜色和大小,但在这个版本中,当我更改画笔的颜色或大小时,当我通过拖动鼠标再次开始绘制时,之前绘制的所有内容也会
我已经实现了可点击的 Recyclerview 项并设置了 android:background="?selectableItemBackground" 以获得点击效果,但是在检查代码时我发现了这个
这是我使用 firemonkey + Delphi XE2 制作的 Delphi 应用程序的示例。 如您所见,您可以使用 Firemonkey 中的视觉样式“样式书”来自定义窗口框架内大多数内容的外观
我想请教您如何在绘画应用程序中实现自由形式的绘画。鉴于命令对象将包含单击、任意拖动和释放,这将如何必然存储在命令中并绘制到位图上下文中? 对于初学者来说,数据是否只是放在一个大列表中的 mousemo
似乎显而易见的答案是否定的,但我还是想找出答案。 如果我的面板中有很多复杂的组件,需要一段时间才能加载,然后在顶部有另一个非常基本的面板,完全覆盖它,背景面板是否仍会在 paint 方法中绘制? 最佳
首先是code: import 'package:flutter/cupertino.dart'; class Test extends StatelessWidget { @override
import java.awt.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.Ac
我的java代码有问题。我想做一个画家程序,但每当我选择一个形状并绘制它时之前绘制的所有形状都变得与此形状相同。这是代码。我知道问题出在 paintComponent 中的 for 语句,但我可以用什
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
这是代码,正方形会渲染但不会移动,即使它应该渲染每个循环。问题似乎出在 Core 类的 run() 中 package com.game; import java.awt.*; import
这里的问题是调用了paintComponent()方法,它获取了fillRect()所需的变量,但在按下按键后实际上并没有绘制任何东西。我不明白为什么,因为每次按下 D 键时 mato.getPosi
程序未在屏幕上显示我的 Oval。我没有收到任何错误,所以我有点停滞不前。我查看了我的另一个程序,我几乎逐字编写了它。 游戏.java public class Game extends JPanel
我是一名优秀的程序员,十分优秀!