WASHINGTON — When President Trump had finished mocking the field of Democratic presidential candidates at a rally in Florida this week (“Sleepy Joe,” “Crazy Bernie” and “Boot-edge-edge”), he pivoted abruptly to his intensifying trade war with China. The segue was no accident: Mr. Trump is determined to present himself as tougher on the Chinese than any of his potential challengers in 2020.

“Representing us against President Xi of China,” a sarcastic Mr. Trump said of Pete Buttigieg, the young mayor of South Bend, Ind. “That’d be great.” Taking aim at former Vice President Joseph R. Biden Jr. earlier in the day, he said that China had pulled back from a trade deal because it wanted to wait him out and negotiate with a President Biden or “one of the very weak Democrats, and thereby continue to rip off the United States.”

Election-year politics have crept into Mr. Trump’s trade policy.

For months, the prospect of a landmark trade agreement with China has tantalized Mr. Trump. But now, according to analysts and several former aides, his political calculus seems to have flipped. His recent statements suggest he now believes that demonstrating his toughness with the Chinese and walking away from a deal might well put him in a better position politically than signing one.

Imposing new tariffs on China is likely to hurt American farmers, rattle the stock market and possibly damage the economy. But signing an agreement could expose Mr. Trump to attacks by Democrats, particularly if it is perceived as weak. A hard line, on the other hand, would allow the president to cater to his political base while heading off any Democratic attempts to outflank him as the great protector of American workers.